Backbone outputing empty array for collection.mode

2019-05-31 21:49发布

问题:

Having a problem where if I do

apples = new Apples
apples.fetch()
console.log apples
console.log apples.models

console.log apples will out put the Apples Collection, with models: Array(10) listed inside the object.

But, console.log apples.models outputs an empty array.

Why does this happen?

Thanks in advance.

回答1:

Are you trying to console.log the collection right after executing the fecth() method or waiting for the callback to be executed by using

apples.on('reset', function(){ console.log(this.models);} ,this); 

?



回答2:

I just tried this and it worked out for me. Backbone collections have a few underscore methods which work out of the box. Try using toArray to get the apple models in the apples collection.

  apples.toArray()