jQuery.when - 当所有延迟不再“未解决"(已解决或被拒绝)时的回调? [英] jQuery.when - Callback for when ALL Deferreds are no longer 'unresolved' (either resolved or rejected)?

查看:19
本文介绍了jQuery.when - 当所有延迟不再“未解决"(已解决或被拒绝)时的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当多个 Deferred 对象被传递给 jQuery.when 时,该方法从一个新的master" Deferred 对象,用于跟踪它已传递的所有 Deferred 的聚合状态.

When multiple Deferred objects are passed to jQuery.when, the method returns the Promise from a new "master" Deferred object that tracks the aggregate state of all the Deferreds it has been passed.

该方法要么

  1. 在所有 Deferred 解析后立即解析其主 Deferred,或者
  2. 一旦其中一个 Deferred 被拒绝,就拒绝其主 Deferred.

如果主 Deferred 被解析(即所有 Deferred 解析),它会传递所有传递给 jQuery.when 的 Deferred 的解析值.例如,当延迟是 jQuery.ajax() 请求时,参数将是请求的 jqXHR 对象,按照它们在参数列表中给出的顺序:

If the master Deferred is resolved (ie. ALL the Deferreds resolve), it is passed the resolved values of all the Deferreds that were passed to jQuery.when. For example, when the Deferreds are jQuery.ajax() requests, the arguments will be the jqXHR objects for the requests, in the order they were given in the argument list:

$.when( $.getJSON('foo'), $.getJSON('bar') ).done(function(foo, bar) {

    // foo & bar are jqXHR objects for the requests

});

在多个延迟的情况下,其中一个延迟被拒绝,jQuery.when 立即触发其主延迟的失败回调,即使此时某些延迟可能仍未解决:

In the multiple Deferreds case where one of the Deferreds is rejected, jQuery.when IMMEDIATELY FIRES the fail callbacks for its master Deferred, even if some of the Deferreds may still be unresolved at that point:

$.when( $.getJSON('foo'), $.getJSON('bar') ).fail(function(req) {

    // req is the jqXHR object for one of the failed requests

});

当所有传递给 jQuery.when 的 Deferreds 不再是未解决"(即所有都是已解决"或拒绝")时,我需要触发回调.我可以发送带有 200 个 OK 代码的 JSON 对象(而不是发送带有 404 Not Found 错误状态代码的 JSON)并在 done() 方法中确定成功/错误,但我更喜欢保持我的 API RESTful.我怎样才能做到这一点?

I need to fire a callback when all the Deferreds passed to jQuery.when are no longer 'unresolved' (ie. all are either 'resolved' or 'rejected'). I could send JSON objects with 200 OK codes (instead sending JSON with 404 Not Found error status codes) and determine success/error in the done() method, but I'd prefer keeping my API RESTful. How can I accomplish this?

推荐答案

我认为最简单的方法是为每个 AJAX 请求保留一个辅助 Deferred 对象,并确保 那个总是解决:

I think the easiest way to do this is to keep a secondary Deferred object around for each AJAX request, and ensure that that one is always resolved:

var d1 = $.Deferred();
var d2 = $.Deferred();

var j1 = $.getJSON(...).complete(d1.resolve);
var j2 = $.getJSON(...).complete(d2.resolve);

$.when(j1, j2).done(function() {
     // only fires if j1 AND j2 are resolved
});

$.when(d1, d2).done(function() {
     // will fire when j1 AND j2 are both resolved OR rejected
     // check j1.isResolved() and j2.isResolved() to find which failed
});

这是利用额外的 AJAX .complete() 方法,jQuery 添加到其对 AJAX 方法的承诺中,该方法被解析和拒绝的承诺调用.

This is making use of the additional AJAX .complete() method which jQuery adds to its promises for AJAX methods, which is called for both resolved and rejected promises.

注意:d1.resolve 本身就是一个回调函数,不需要包裹在 function() { ... } 块中.

NB: d1.resolve works as a callback in its own right, it doesn't need to be wrapped in a function() { ... } block.

这篇关于jQuery.when - 当所有延迟不再“未解决"(已解决或被拒绝)时的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆