简体   繁体   English

使用jQuery的when __each多个ajax调用上的回调

[英]using jquery's when for a callback on multiple ajax calls with _.each

I saw some examples on wrapping ajax calls with jquery's when function. 我看到了一些使用jquery的when函数包装ajax调用的示例。 As I'm just getting started with JS, I'm unsure on how to do this with a _.each. 当我刚开始使用JS时,我不确定如何使用_.each做到这一点。 All the examples were cases where they just created an array and wrapped it in jquery's when. 所有示例都是在它们只是创建一个数组并将其包装在jquery的when中的情况。 Right now, the code basically goes: 现在,代码基本上是这样的:

_.each(this.models, function (model) {
                model.fetch({
                    success: function (model, response) {
                        // do something                    },

How can I wrap this with jquery's when to get a callback when everything is finished? 当一切都完成后,如何用jquery的when包装它以获取回调? Thanks! 谢谢!

var requests = _.map(this.models, function (model) {
        return model.fetch({
            success: function (model, response) {
            }
        });
});

//Working around the fact that array is not accepted
$.when.apply($, requests).then(function(){

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM