我正在尝试遍历集合获取的模型。collection.each()不遍历模型
我有如下因素的一段代码:
initialize: function() {
this.collection = new UserCollection();
this.collection.fetch();
this.render();
},
renderCollection: function() {
console.log("rendering collection");
this.collection.each(function(index,model){
console.log("model");
});
console.log(this.collection);
},
render: function() {
this.template = _.template(template, {});
this.$el.html(this.template);
// some other stuff
this.renderCollection();
}
和结果:
rendering collection
d {models: Array[0], length: 0, _byId: Object, constructor: function, model: function…}
_byId: Object
_idAttr: "id"
length: 4
models: Array[4]
0: d
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
created: "2013-02-13 09:22:42"
id: "1"
modified: "2013-02-13 09:22:42"
role: "admin"
username: "[email protected]"
__proto__: Object
changed: Object
cid: "c5"
collection: d
id: "1"
__proto__: e
1: d
2: d
3: d
length: 4
__proto__: Array[0]
__proto__: e
user_list.js:25
所以取方法做的工作 - 在对象转储我能找到4条牵强,但遍历集合呢不工作...
该问题可能来自服务器输出,需要是一个有效的json对象数组。即在PHP中索引json_encoded数组 – 2014-03-24 08:48:16
您的迭代器应该是: 'this.collection.each(function(model){...});' 或 'this.collection.each(function(model,index){...});' OP有索引和模型错误的方式。 – 2014-04-30 13:46:38
我不得不做每一个collection.models这样(不是集合):http://stackoverflow.com/questions/11726943/for-loop-over-backbone-collection...这是我的问题。 – Kelly 2014-05-25 15:50:20