我正在尝试迭代通过集合获取的模型。
我有以下代码:
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@gmail.com"
__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
所以 fetch 方法确实有效 - 在对象转储中我可以找到 4 条记录,但迭代集合不起作用......