我正在处理我的第一个backbone.js 问题,并且在使用集合时遇到了困难。从控制台来看,问题似乎出在将集合视图输出到 dom 中。收集数据在控制台中正确格式化,只是没有显示在浏览器中。我不知道我做错了什么:
var RegisterModel = Backbone.Model.extend({
urlRoot: 'api/process.php'
});
var RegisterView = Backbone.View.extend({
el: "#register",
template: _.template('<label for="<%= inputName %>"><%= label %></label><input name="<%= inputName %>" type="text" />'),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
var RegisterCollection = Backbone.Collection.extend({
model: RegisterModel
});
var RegisterCollectionView = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
this.collection.on('reset', this.addAll, this);
},
addOne: function(registerModel){
var registerView = new RegisterView({model: registerModel});
this.$el.append(registerView.render().el);
},
addAll: function(){
this.collection.forEach(this.addOne, this);
this.$el.empty();
},
render: function(){
this.addAll();
return this;
}
});
var registerCollection = new RegisterCollection();
var todos = [{inputName: "first_name", label: "First Name"}, {inputName: "last_name", label: "Last Name"}];
registerCollection.reset(todos);
var registerCollectionView = new RegisterCollectionView({collection: registerCollection});
registerCollectionView.render();