14 Jun 2010

jQuery Draggable/Droppable: Revert on drop

Posted by Jacob Emcken

Background:

At work we are currently in the process of creating a general data hub which is easily expendable and configurable. It’s already in use exchanging EDI messages (EDIFACT) for a supplier on the danish electricity market.

For the configuration we settled on a GUI where you could drag different kinds of data manipulations into the data flow and this is all implemented in HTML with jQuery.

The problem:

Upon drop I had to use Ajax to ask the server if it was ok to drop the draggable. Since Ajax is asynchronous the drop event would return before the Ajax actually had finished. I decided that I didn’t want to force the request to the server to be synchronous instead I wanted the ajax success callback to be able to do the reverting.

Upon searching the net, the closest thing I came to a solution was defining a callback function for revert on the draggable, and this functionality isn’t even documented by the time of this writing.

The solution:

After playing a bit around I found an acceptable solution. I’ve created a small example reverting a drop using a confirm where you can test it (and see the code).

Tags: , , ,

2 Comments to jQuery Draggable/Droppable: Revert on drop

Kasper Johansen
August 5, 2010

Hi Jacob.

Not sure if I understand your problem rigth, but have you tried turning async off like this:

$.ajax((type: “GET”, url: “…”, async: false…

That way the function wont return before the callback is done.

Jacob Emcken
August 8, 2010

Yes I know but I think this would lock the UI until a response is returned.

This is what discouraged me:

By default, all requests are sent asynchronous (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: “jsonp” requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

Source: http://api.jquery.com/jQuery.ajax/

Leave a comment