I am trying to run multiple fetchs to populate a collection which is a property of a model.
I receive all information I want to get, but as soon as my function is done every group has the same members (the members which have been fetched the last).
I placed two console.log statements in the code to se where the informations are right the last and when they get wrong. Nevertheless, I have no idea what I can do to store the information write.
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;