Jquery ui 如何从jquery回调到父函数中获取数据
我正在使用jQueryUI拖放代码。一旦删除,就会执行一个getJSON请求来检查新数据并更新数据库。这可以正常工作,直到我的后端返回错误,因为我无法取消匿名函数中的删除 如果出现错误,后端将返回如下所示的json:Jquery ui 如何从jquery回调到父函数中获取数据,jquery-ui,callback,scope,droppable,Jquery Ui,Callback,Scope,Droppable,我正在使用jQueryUI拖放代码。一旦删除,就会执行一个getJSON请求来检查新数据并更新数据库。这可以正常工作,直到我的后端返回错误,因为我无法取消匿名函数中的删除 如果出现错误,后端将返回如下所示的json: {"result":0} 这是处理丢弃的代码: $('.droppable').droppable({ drop: function(event, ui) { $.getJSON('/roster/save', 'location_id=1', function ()
{"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));
}});
我希望这是有点清楚。
有什么想法吗?:) 在找到一个不那么难看的解决方案之前,我将使用同步ajax调用和jQuery.data()方法。因此,与.getJSON()调用不同: 然后,我可以检查存储数据的值,并在必要时返回false:
if (this).data('tmp').result != 1) {
return false;
}
希望这对其他人有所帮助。为什么不将json数据分配给一个新的全局变量,该变量应该在getJson函数之外可用
$('.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));
}
});
$('.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));
}
});