您如何使用jQuery Deferreds数组? [英] How do you work with an array of jQuery Deferreds?

查看:105
本文介绍了您如何使用jQuery Deferreds数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要按一定顺序加载数据的应用程序:根URL,然后是架构,然后最终使用各种数据对象的架构和url初始化应用程序.当用户浏览应用程序时,数据对象将被加载,针对架构进行验证并显示.当用户对数据进行CRUD时,这些模式将提供首过验证.

I have an application that requires data be loaded in a certain order: the root URL, then the schemas, then finally initialize the application with the schemas and urls for the various data objects. As the user navigates the application, data objects are loaded, validated against the schema, and displayed. As the user CRUDs the data, the schemas provide first-pass validation.

我在初始化时遇到问题.我使用Ajax调用来获取根对象$ .when(),然后创建一个promise数组,每个模式对象一个.那个有效.我在控制台中看到了抓取内容.

I'm having a problem with initialization. I use an Ajax call to fetch the root object, $.when(), and then create an array of promises, one for each schema object. That works. I see the fetch in the console.

然后,我将看到所有模式的访存,因此每个$ .ajax()调用均有效. fetchschemas()实际上确实返回了一个promise数组.

I then see the fetch for all the schemas, so each $.ajax() call works. fetchschemas() does indeed return an array of promises.

但是,最终的when()子句永远不会触发,并且"DONE"一词也不会出现在控制台上. jquery-1.5的源代码似乎暗示空"可以作为传递给$ .when.apply()的对象,因为when()将构建内部Deferred()对象来管理列表(如果没有对象的话)通过.

However, that final when() clause never fires and the word "DONE" never appears on the console. The source code to jquery-1.5 seems to imply that "null" is acceptable as an object to pass to $.when.apply(), as when() will build an internal Deferred() object to manage the list if no object is passed in.

这是使用Futures.js进行的.如果不是这样,应该如何管理jQuery Deferred数组?

This worked using Futures.js. How should an array of jQuery Deferreds be managed, if not like this?

    var fetch_schemas, fetch_root;

    fetch_schemas = function(schema_urls) {
        var fetch_one = function(url) {
            return $.ajax({
                url: url,
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "json"
            });
        };

        return $.map(schema_urls, fetch_one);
    };

    fetch_root = function() {
        return $.ajax({
            url: BASE_URL,
            data: {},
            contentType: "application/json; charset=utf-8",
            dataType: "json"
        });
    };

    $.when(fetch_root()).then(function(data) {
        var promises = fetch_schemas(data.schema_urls);
        $.when.apply(null, promises).then(function(schemas) {
            console.log("DONE", this, schemas);
        });
    });

推荐答案

您正在寻找

$.when.apply($, promises).then(function(schemas) {
     console.log("DONE", this, schemas);
}, function(e) {
     console.log("My ajax failed");
});

这也将起作用(对于某些有价值的工作,它不会修复损坏的ajax):

This will also work (for some value of work, it won't fix broken ajax):

$.when.apply($, promises).done(function() { ... }).fail(function() { ... });` 

您将要传递$而不是null,以便$.when中的this引用jQuery.无关紧要的是源,但是最好传递null.

You'll want to pass $ instead of null so that this inside $.when refers to jQuery. It shouldn't matter to the source but it's better then passing null.

$.when替换掉所有$ .ajax,然后示例工作

Mocked out all your $.ajax by replacing them with $.when and the sample works

所以这可能是您的ajax请求中的问题,或者是传递给fetch_schemas的数组.

So it's either a problem in your ajax request or the array your passing to fetch_schemas.

这篇关于您如何使用jQuery Deferreds数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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