Backbone outputing empty array for collection.models?

耗尽温柔 提交于 2019-12-23 02:36:14

问题


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


来源:https://stackoverflow.com/questions/11954243/backbone-outputing-empty-array-for-collection-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!