[英]How to pass a args from a jQuery defered
I have a Backbone model that fetch some data, process the data and then a function should get the processed data. 我有一个Backbone模型,该模型可获取一些数据,处理数据,然后一个函数应获取处理后的数据。
$.when(model.fetch())
.done(function(){
return model.processData()
})
.then(function(processedData){
//make something with the processed data
})
Unfortunately the then
method get the result from the model.fetch()
call and not the return value of the done
function 不幸的是, then
方法从model.fetch()
调用中获得结果,而不是done
函数的返回值。
You have to use .then
instead of .done
. 您必须使用.then
而不是.done
。 .then
returns a new promise which is resolved with the value the callback function returns. .then
返回一个新的promise,它用回调函数返回的值解析。
.done
on the other hand returns the original promise object and the return value of the callback is simply ignored. 另一方面, .done
将返回原始的Promise对象,并且回调的返回值将被忽略。
More information can be found in the documentation (emphasize mine): 可以在文档中找到更多信息(强调我的):
As of jQuery 1.8, the
deferred.then()
method returns a new promise that can filter the status and values of a deferred through a function, replacing the now-deprecateddeferred.pipe()
method. 从jQuery 1.8开始,deferred.then()
方法返回一个新的promise,可以通过函数过滤deferred的状态和值,从而替换了现在不推荐使用的deferred.pipe()
方法。 ThedoneFilter
andfailFilter
functions filter the original deferred's resolved / rejected status and values.doneFilter
和failFilter
函数过滤原始延迟的已解析/已拒绝状态和值。 TheprogressFilter
function filters any calls to the original deferred'snotify
ornotifyWith
methods.progressFilter
函数过滤对原始延迟的notify
或notifyWith
方法的所有调用。 These filter functions can return a new value to be passed along to the promise's.done()
or.fail()
callbacks , or they can return another observable object (Deferred, Promise, etc) which will pass its resolved / rejected status and values to the promise's callbacks. 这些筛选器函数可以返回要传递给.fail()
的.done()
或.fail()
回调的新值 ,也可以返回另一个可观察对象(Deferred,Promise等),该对象将传递其已解析/拒绝的状态和值到诺言的回调。 If the filter function used isnull
, or not specified, the promise will be resolved or rejected with the same values as the original. 如果使用的过滤器函数为null
或未指定,则将以与原始值相同的值来解析或拒绝promise。
To intercept , modify or filter data from a done
handler to further calls, you can use the .pipe()
method. 若要拦截 , 修改或过滤来自done
处理程序的数据以done
进一步的调用,可以使用.pipe()
方法。
Even if in your example, you could just entirely replace the .done()
, with your .then
handler directly, it could also look like: 即使在您的示例中,您可以直接用.then
处理程序完全替换.done()
,它也可能看起来像:
$.when(model.fetch())
.pipe(function() {
return model.processData();
})
.then(function(processedData){
//make something with the processed data
});
Of course this doesn't make too much sense as mentioned above, but think about the possibilites of .pipe()
anyway. 当然,如上所述,这样做没有太大意义,但是无论如何.pipe()
考虑.pipe()
的可能性。 It can be a very handy tool. 它可能是一个非常方便的工具。
$.when()
should not be necessary. $.when()
不必要。
Here are two possibilities : 这有两种可能性:
model.fetch().then(function() {
return model.processData()
}).then(function(processedData){
//make something with the processed data
});
or, if processData()
is synchronous : 或者,如果processData()
是同步的:
model.fetch().done(function() {
var foo = model.processData();
//make something with the processed data, foo
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.