0

jqueryuiのドラッグアンドドロップコードを使用しています。ドロップすると、getJSONリクエストが実行され、新しいデータをチェックしてデータベースを更新します。匿名関数内からドロップをキャンセルできないため、バックエンドがエラーを返すまで、これは正常に機能します。

エラーが発生した場合、バックエンドは次のようなjsonを返します。

{"result":0}

ドロップを処理するコードは次のとおりです。

 $('.droppable').droppable({
  drop: function(event, ui) {
   $.getJSON('/roster/save', 'location_id=1', function (){
    if (data.result==0) {
     // now the drop should be cancelled, but it cannot be cancelled from the anonymous function
    }
   });

   // if data was available here I could check the result and cancel it
   if (data.result==0)
    return false; // this cancels the drop

   // finish the drop
   ui.draggable.appendTo($('ul', this));
  }});

これが少し明確であることを願っています。何か案は?:)

4

2 に答える 2

0

まあ、もっと醜い解決策が見つかるまでは、jQuery .data() メソッドと組み合わせて同期 ajax 呼び出しを使用します。したがって、.getJSON() 呼び出しの代わりに:

$.ajax({  
 async: false,
 type: "POST",  
 url: "/roster/add",  
 data: 'location_id=1',  
 dataType: 'json',
 success: $.proxy(function(data) { $(this).data('tmp',data) }, $(this)),
});  

その後、保存されたデータの値を確認し、必要に応じて false を返すことができます。

if (this).data('tmp').result != 1) {
 return false;
}

これが誰かに役立つことを願っています。

于 2010-09-27T15:16:14.330 に答える
0

json-data を新しいグローバル変数に割り当てて、getJson-function の外部で使用できるようにしてみませんか?

$('.droppable').droppable({
    drop: function(event, ui) {
        $.getJSON('/roster/save', 'location_id=1', function (){
            jsonData = data;
        });

        // if data was available here I could check the result and cancel it
        if (jsonData.result==0)
        return false; // this cancels the drop

        // finish the drop
        ui.draggable.appendTo($('ul', this));
    }
});
于 2010-09-27T15:27:57.640 に答える