我有以下简化代码:
$.when(someClass.MethodWithXhrCall(args, callBack, errCallBack))
.then(function () {
console.log('Yep');
}
})
.fail(function () {
console.log('Nope');
});
function callBack(data) {
// Yes I got my data
var x=data.CustomerName;
.....
}
function errCallBack(data) {
alert (data.ErrorText);
}
未调用回调。然而,当我不使用延迟构造时,回调按预期工作。当然,我遇到了其他时间问题,我试图避免使用延迟构造。
*更新于 2013 年 5 月 23 日 [已解决] * 我终于可以做我想做的事了。我的回调正在获取响应对象,而延迟对象正在阻止要执行的代码,直到调用完成。
这是代码:
// Define this as a global variable
var _eipDfd = null; // Will be used to create the deferred object
// Call the webservice to read this customer's data
console.log("Reading existing customer data");
function asyncEvent() {
_eipDfd = new jQuery.Deferred();
someClass.GetCustomer(args, processReadCust, errCallBack);
return _eipDfd.promise();
}
$.when(asyncEvent()).then(
function (status) {
console.log(status); // Will print Success!!
},
function (status) {
console.log(status); // Will print Failed :(
}
);
console.log("Web service call is done");
......
// Callback functions are still being called from someClass.GetCustomer with the response object being pass to them
processReadCust: function (data) {
// Do the work
_eipDfd.resolve("Success!!");
return;
}
errCallBack: function (data) {
// Take care of failure
_eipDfd.reject("Failed :(");
return;
}