Model attributes not getting set correctly after a fetch

77 Views Asked by At

In my model I have some nested collections, the API returns some data, and this gets parsed into a model fine, however because my API returned arrays in objects some of my attributes tend to look like this when logged...

member: Array[2]

In my model I am doing the following...

initialize: function() {

    //Gets
    var members  = this.get('members');
    //this.unset('members');

    //Sets
    this.set('members', new App.Collections.Users(members));

},

Now the members field does not get set to the collection when a fetch is done, why is this? I would have thought in fetching a model I am instantiating one? At the moment I have to run model.initialize() after I have done my fetch, is there a way around this?

1

There are 1 best solutions below

0
On

members is already set after fetch. Also, initialize is NOT called after fetch.

If you want to perform any action after fetching, then something like this can be done :

myModel.fetch().done(function(){
    this.set('members',new App.Collections.Users(this.get('members'));
});