jQuery Deferred - 在运行时向延迟契约添加回调 [英] jQuery Deferred - Add callbacks to the Deferred contract at runtime

查看:59
本文介绍了jQuery Deferred - 在运行时向延迟契约添加回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是添加一个未知的数字(仅在运行时知道).pipe()调用jQuery的.when()监视的函数。这些调用将基于由于另一个异步操作而产生的ajax调用。请参阅下面的代码以获得更清晰的解释:

I need is to add an unknown number (known only at runtime) of .pipe() calls to the functions being monitored by jQuery's .when(). These calls will be based off of ajax calls made as a result of another asynchronous operation. Please see the code below for a more clear explanation:

$.when(
    $.ajax({ 
        async: true,        
        success: function (data, textStatus, jqXhr) {
            console.log('Level 1')

            for(var i = 0; i < data.length; i++)
                jqXhr.pipe(
                    $.ajax({  
                        data: data[i],                                                              
                        async: true,        
                        success: function (data, textStatus, jqXhr) {
                            console.log('Level 2');
                        },       
                    })
                );
        },       
    }),
    $.ajax({
        async: true,        
        success: function (data, textStatus, jqXhr) {
            console.log('Level 3');
        },       
    })
).done(function(){ console.log('All done!'); });

基本上,1级和3级需要并行执行。级别2都基于级别1的结果。级别1,所有级别2和级别3都需要在完成所有之前执行。

Basically Level 1 and Level 3 need to execute in parallel. The Level 2s are all based off of the result of Level 1. And Level 1, all the Level 2s and Level 3 need to execute before All done.

使用上面的代码不起作用,因为对.pipe()的调用不会影响.when()监视的内容。

Using the code above doesn't work, as the call to .pipe() does not affect what .when() is monitoring.

用jQuery的Deferred框架可以做我想做的事吗?

Is it possible to do what I want with jQuery's Deferred framework?

感谢您的帮助。

注意:早些时候,我问过一个非常相似的问题,但我已经意识到情况比我在那里说的要复杂得多,我不想引起混淆现有答案。

Note: Earlier, I asked a very similar question, but I've realized the situation is significantly more complicated than what I illustrated there and I didn't want to cause confusion with the existing answers.

原始问题

推荐答案

它并没有那么复杂。如果你想并行执行所有2级调用,只需在 .pipe 回调中调用 $。时并返回延迟对象:

It's not that much more complicated. If you want to execute all level 2 calls in parallel, just call $.when inside the .pipe callback and return that deferred object:

$.when(
    $.ajax({        
        // ...      
    }).pipe(function(data) {
        // concise way to make the Ajax calls for each value
        var deferreds = $.map(data, function(value) {
            return $.ajax({data: value, /*...*/});
        });
        return $.when.apply($, deferreds);
    }),
    $.ajax({        
        // ...       
    })
).done(function(){ console.log('All done!'); });

如果要按顺序执行它们,请使用 .pipe 再次:

If you want to execute them sequentially, use .pipe again:

$.when(
    $.ajax({        
        // ...      
    }).pipe(function(data) {
        // A "dummy" resolved deferred object 
        var deferred = (new $.Deferred()).resolve();
        $.each(data, function(value) {
            deferred = deferred.pipe(function() {
                return $.ajax({data: value, /*...*/});
            });
        });
        return deferred;
    }),
    $.ajax({        
        // ...       
    })
).done(function(){ console.log('All done!'); });






我不得不说你应该保留Ajax调用次数最少。


I have to say though that you should keep the number of Ajax calls to a minimum.

这篇关于jQuery Deferred - 在运行时向延迟契约添加回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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