在我的ZenDesk应用程序中,我:
使用普通jQuery,您可以使用jQuery.when(deferreds)进行协调,以便在步骤2中的所有请求完成后触发第3步:
$.when($.ajax("/page1"), $.ajax("/page2"))
.done(function(page1result, page2result) {
// modify the ticket with the results
});
this.$.when()
没有运气。)答案 0 :(得分:5)
jQuery.when()
可通过应用程序对象this.when()
获得。这是一个简单的示例(框架版本0.5),它创建了一些简单的承诺(使用this.promise()
,类似于jQuery.Deferred()
)然后等待它们成功/解析以调用第三个函数。
用this.ajax(...)
替换this.createPromise()
来做实际工作。
(function() {
return {
onActivate: function() {
var promises = []
promises.push(this.createPromise().done(function() {
console.log('promise 1 done');
}));
promises.push(this.createPromise().done(function() {
console.log('promise 2 done');
}));
this.when.apply(this, promises).done(function() {
console.log('all promises done');
});
},
// returns a promise that always succeeds after 1 sec.
createPromise: function() {
return this.promise(function(done, fail) {
setTimeout(done, 1000);
});
},
events: {
'app.activated': 'onActivate'
}
};
}());