[英]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
)? 是否可以將任何d1
, d2
,..., 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.