2483409_icon_small Jeff Cohen 4 posts

Hi,

Just wondering if there’s any known technique to cancel the revert action of a draggable depending on where it’s dropped?

I’m using Rails to build a simple chess app as a demo for a Rails class. Players can drag pieces to move them on the board. I’m successfully using the accept parameter to help restrict where a piece can land (empty square or enemy square). If dropped on an acceptable square, the drop succeeds and it calls the controller as expected.

But when dropped on an invalid square, the controller is not called (which is good), but the piece remains where it was dropped (which is bad). I’d like the piece to be reverted if the drop is not on an acceptable region. If I use :revert => true on the draggable element, it will always revert even on a valid move, which causes a flash effect since it reverts and then the board updates with the new position right after that.

I actually expected this to be the default behavior when I first learned about the accept option using CSS classes. If a piece is not dropped on an acceptable region, then of course it should be reverted, no?

Is there any way to achieve this functionality, either with RJS (hopefully?) or with direct Javascript?

Thanks!
Jeff

 
Headshot_120px_small Christophe P... 28 posts

Hey Jeff,

I believe the special 'failure' value for the revert option is exactly what you’re looking for: it will trigger the revert effect only if the drop failed (if the piece was refused on dropping). This occurs after any custom checking on the Dropppables side (including custom accept/@containment@…

Let me know if that cut it out for you!

Cheers,

 
2483409_icon_small Jeff Cohen 4 posts

That worked perfectly! Awesome.

And now I see it’s explained on p. 289 in the description of the revert option. I totally missed it before even though I read that section.

Thanks again.
Jeff

 
Headshot_120px_small Christophe P... 28 posts

Glad I could help. Keep reading! :-)

4 posts, 2 voices