在我看来,类初始化函数_.bind(this.appendSection, this)
不起作用,但_.bindAll(this, 'appendSection')
有效。我很迷茫...
这是代码:
TemplateBuilder.Views.TemplateView = Backbone.View.extend({
el: $('div#evalTemplate'),
initialize: function(){
this.collection.on('reset', this.render, this);
//_.bind(this.appendSection, this);
_.bindAll(this, 'appendSection');
},
events: {
'click button#addSection': 'addSection'
},
render: function(){
this.collection.each(this.appendSection);
return this;
},
appendSection: function(section){
var view = new TemplateBuilder.Views.InstructionView({model: section});
this.$el.append(view.render().el);
},
addSection: function(){
var newSection = new TemplateBuilder.Models.Section();
this.collection.add(newSection);
this.appendSection(newSection);
},
});