9-bits
9-bits

Reputation: 10765

fetch is not updating collection.model.models

I have a backbone collection that i've initialized like this:

myCollection = new MyCollection([], {type: 'animals', params: {username: 'steve'}});

myCollection.fetch();

console.log(myCollection)  // prints out an object that includes 'models' and the newly fetched models

console.log(myCollection.models)  // prints out an empty list []

does anyone know why?

Upvotes: 0

Views: 80

Answers (2)

Daniel Aranda
Daniel Aranda

Reputation: 6552

The model of your collection must have an url to the server to fetch it into a collection, my thought that you have it on "MyCollection", just in case. And then you only need to add a success callback to display the collection populated, like this:

myCollection = new MyCollection([], {type: 'animals', params: {username: 'steve'}});

myCollection.fetch({
  success : function(returnedCollection, response){
       console.log(returnedCollection);  

        console.log(returnedCollection.models);
  }
});

Upvotes: 0

jakee
jakee

Reputation: 18556

fetch is an asynchronous operation so whatever you do immediately after fetch is most likely executed before the fetch is finished, which leads to quite random results. put the console logging inside the fetch's success-function and see what happens

Upvotes: 2

Related Questions