簡體   English   中英

Dojo XHR鏈

[英]Dojo xhr chaining

我有以下延遲對象:

var base = xhr.get({
    url: config.baseUrl + base_query,
    handleAs: "json",
    load: function(result) {
        widget.set('value', result);
    },
    error: function(result) {
    }
});

完成此GET請求后,我需要使用使用第一個base結果的URL執行第二個請求:

var d1 = base.then(
    function(result) {
        xhr.get({
            url: config.baseUrl + result.id,
            handleAs: "json",
            load: function(result) {
                widget.set('visibility', result);
            },
            error: function(result) {
            }
        })
   },
   function(result) {
   }
);

工作正常。 但是,如何根據base結果而不是發出兩個或多個請求(例如d1 )? 是否可以將任何d1d2 ,..., dn到一個延遲對象中, then使用then將其連接到base對象?

對,就是這樣。 您可以致電then在無限次base

var d1 = base.then(fn1),
    d2 = base.then(fn2),
    …

請注意,盡管當前d1可能工作正常,但您的d1並不代表任何結果-鏈已斷開,因為您沒有從回調中返回任何內容。 您應該實際返回第二個請求的承諾:

var base = xhr.get({
    url: config.baseUrl + base_query,
    handleAs: "json"
});
base.then(widget.set.bind(widget, 'value'));
// or:    dojo.hitch(widget, widget.set, 'value') if you like that better

var d1 = base.then(function(result) {
    return xhr.get({
//  ^^^^^^
            url: config.baseUrl + result.id,
            handleAs: "json"
    });
});
d1.then(widget.set.bind(widget, 'visibility'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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