无法遍历骨干网集合 [英] Can't loop over backbone collection
问题描述
看来我无法遍历一个骨干网集合.我已经在多个线程中看到了该主题,但是这些解决方案都无济于事.
It seems that I can't loop through a backbone collection. I've seen this topic in several threads, but none of those solutions help.
render:function () {
this.$el.html(this.template(this.model.attributes));
var that = this;
console.log(this.projects.models);
_.each(this.projects.models, function(model) { console.log(model); });
return this;
}
从此,我的控制台仅显示Array [2] 我希望也会看到每个模型. 有人知道我在做什么错吗?
From this my console only shows Array[2] I would expect to see each model as well. Does anyone know what I'm doing wrong here ?
推荐答案
要以数组形式获取集合内容,请使用方法toJSON
(例如:collection.toJSON()
)
To get your collection content as an array, use the method toJSON
(e.g.: collection.toJSON()
)
接下来,要遍历整个集合,请在集合实例上使用each
方法!
Next, to loop over your entire collection, use the each
method on the collection instances!
collection.each(function( model ) {
console.log(model)
});
如果没有显示完整的收藏夹,那么问题就出在如何在收藏夹中添加项目,而不是循环逻辑中.
If this don't show your full collection, then the problem is in how you add items inside your collection, not in the looping logic.
这篇关于无法遍历骨干网集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!