0

I am using redips drag and drop jquery plugin and I would like to cancel drop when status from server is false.
Here is the sample of my code.

My system works like this, when you drag an element, I send data to server to check, if it's successfully checked, it will return with true or false. if it is false I should cancel drop and the element return to the previous location.

var redipsInit;  // define redipsInit variable
redipsInit = function () {
 var rd = REDIPS.drag;     
 rd.init();  
 rd.hover.colorTd = '#9BB3DA'; 
 rd.event.moved = function (){
 var objOld = rd.objOld;
 REDIPS.drag.dropMode ='switch';
 };
 // element is dropped
 rd.event.droppedBefore = function () {
 var data  //are data i send to server to process
 $.ajax({
         url: 'http://127.0.0.1/server/2',
         data: data,
         type: "get",
         cache: false,
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         timeout:20000,
         success: function (data) {

          var status = data.status;

          if(status == true){

          return true; /*if status is true i should drag and drop                       successfully*/

          } else{
             var errors =data.errorjson;
            if(errors == 1){

              alert("alert");
              return false;//here is the problem, it is always returning true instead of false in order to cancel my drop 

            }
                }
         },
         error: function (xhr, ajaxOptions, thrownError) {

          alert("alert");
          return false;
         }
         });
 };

};
Johan
  • 74,508
  • 24
  • 191
  • 319
  • 1
    This is just basically just an AJAX request. Can you [edit] the question to add in the code which is calling this, related to where you are doing the drag-drop? – Rhumborl Jun 06 '16 at 13:19
  • @Adeneo here i have already receive data from server, i receive either succcess true or false.in case it is false i should return false to cancel drop – fred mwimike Jun 06 '16 at 13:24
  • Is `$.ajax()` called at `dragstart` event? Can you create a jsfiddle http://jsfiddle.net to demonstrate? – guest271314 Jun 06 '16 at 13:28
  • thank you, i am using redips – fred mwimike Jun 06 '16 at 13:44
  • @fredmwimike - the problem is that you're waiting for the ajax request, and when it finishes it's too late to return anything to the `droppedBefore` event, as the item is already dropped while you were waiting for a response from the **asynchronous** call. – adeneo Jun 06 '16 at 19:25

0 Answers0