[英]Can I pass Promises to jQuery.when(), or only Deferreds?
[英]jQuery.when(deferreds) or equivalent in ZenDesk App
在我的ZenDesk應用程序中,我:
使用普通的jQuery,你可以使用jQuery.when(deferreds)協調它,一旦步驟2中的所有請求都完成,就觸發第3步:
$.when($.ajax("/page1"), $.ajax("/page2"))
.done(function(page1result, page2result) {
// modify the ticket with the results
});
this.$.when()
沒有運氣。) jQuery.when()
可以通過應用程序對象獲得, this.when()
。 這是一個簡單的例子(框架版本0.5),它創建了幾個簡單的promise(使用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'
}
};
}());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.