簡體   English   中英

jQuery.when(deferreds)或ZenDesk App中的等價物

[英]jQuery.when(deferreds) or equivalent in ZenDesk App

在我的ZenDesk應用程序中,我:

  1. 從故障單和請求者檢索一些識別信息
  2. 向另一個Web服務發出多個請求
  3. 使用組合結果修改故障單

使用普通的jQuery,你可以使用jQuery.when(deferreds)協調它,一旦步驟2中的所有請求都完成,就觸發第3步:

$.when($.ajax("/page1"), $.ajax("/page2"))
    .done(function(page1result, page2result) { 
        // modify the ticket with the results
    });
  1. 是否在應用程序中提供了jQuery.when()? (我試過this.$.when()沒有運氣。)
  2. 如果沒有,那么完成類似事情的首選方式是什么? (也許直接使用Promises ?)

jQuery.when()可以通過應用程序對象獲得, this.when() 這是一個簡單的例子(框架版本0.5),它創建了幾個簡單的promise(使用this.promise() ,類似於jQuery.Deferred() )然后等待它們成功/解析以調用第三個函數。

this.ajax(...)代替this.createPromise()來做實際工作。

app.js

(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM