Backbone outputing empty array for collection.mode

2019-05-31 21:53发布

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.

2条回答
看我几分像从前
2楼-- · 2019-05-31 22:10

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()
查看更多
Evening l夕情丶
3楼-- · 2019-05-31 22:30

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); 

?

查看更多
登录 后发表回答