使用 Backbone.js,我试图将我的数据引导到一个集合中。我的引导程序看起来像log.reset( <JSON> );
当我 console.log 我的收藏时,我看到一堆充满数据的对象。问题是当我对循环中的单个模型进行控制台登录时,我看到一堆没有数据的空对象。数据都在集合中,但我无法在每次 forEach 传递时将其传递到我的模板中。我在项目的另一部分使用完全相同的代码结构来渲染一个完美运行的集合视图......我不知道为什么这个不起作用。
请注意,我正在侦听集合上的重置事件。
//Log Model
var LogItem = Backbone.Model.extend({});
var logItem = new LogItem();
//Log Collection
var Log = Backbone.Collection.extend({
model: LogItem
});
var log = new Log();
//Log Item View
var LogItemView = Backbone.View.extend({
tagName: 'tr',
template: _.template($('#log-item-template').html()),
render: function(){
var attributes = this.model.toJSON();
this.$el.html(this.template( attributes ));
console.log( this.model.toJSON() ); //This shows a bunch of empty objects
return this;
}
});
//Log View
var LogView = Backbone.View.extend({
el: '#log',
initialize: function() {
this.collection.on('reset', this.render, this);
},
render: function(){
this.addAll();
console.log( this.collection.toJSON() ); //This shows objects full of data
},
addOne: function(food) {
var logItemView = new LogItemView({model: logItem});
this.$el.append(logItemView.render().el);
},
addAll: function() {
this.$el.html('');
this.collection.forEach(this.addOne, this);
}
});
var logView = new LogView({collection: log});
如何修复我的空模型?