在骨干中使用延迟的嵌套提取

时间:2013-07-02 08:48:11

标签: backbone.js fetch jquery-deferred backbone-collections



我正在尝试运行多个fetch来填充一个集合,这是一个模型的属性。 我收到了我想要获得的所有信息,但是一旦我的功能完成,每个组都有相同的成员(最后获取的成员)。

我在代码中放置了两个console.log语句,以便在信息最后和出错时将信息放在哪里。不过,我不知道我能做些什么来存储信息。

    var dfr = $.Deferred();
    var that = this;
    that.get('groups').fetch({
        data: $.param({ match_id: that.get('match_id')}),
        success: function(response) {
            response.each(function(obj, i) {
                 $deferreds[i] = $.Deferred();

                 obj.loadMembers(loadRatings).done(function(){
                    $deferreds[i].resolve();
                     console.log(obj.get("group_members")) // HERE ALL GROUPS ARE RIGHT
                });
            });

            $.when.apply(null,$deferreds).then(function(){
                console.log(that.get("groups")) // HERE ALL GROUPS HAVE THE SAME MEMBERS
                dfr.resolve();
            });
        },
        error: function (model, response) {
          dfr.fail("could not load users");
        }
    })
    return dfr;     

0 个答案:

没有答案
相关问题