我有一个与一个Model
关联的EditorView子视图,它呈现一系列表单元素(input checkbox,textarea)。骨干事件被多次触发
我的ListView创建一个也是唯一一个编辑器子视图。 ListView通过点击ListView中的项目来创建编辑器视图,方法是new EditorView(model: this.model). render();
。
这很好。
但是,当事件被连接到编辑子视图
// Bind to all properties in the view
events: {
"click input.isactive": "editActive"
},
会触发该事件多次......每一次以前加载EditorViews。就好像许多HTML仍然存在一样。但是检查DOM(在Firefox中)只显示一个EditorView的html。
我在EditorView中使用.html(字符串),我认为它会替换'el'元素中的前一个html。
var compiledTemplate = _.template(Template, data); // Merge model with template
this.$el.html(compiledTemplate); // replace the previous html if any. ?? OR NOT!! SOAD
感谢
的EditorView
el: ".editor",
tagName: "ul",
className : "striped",
events: {
"click .isactive": "editActive"
},
render : function() {
var data = {
item: this.model,
_: _
};
var compiledTemplate = _.template(Template, data); // Merge model with template
this.$el.empty().html(compiledTemplate); // replace the previous html
return this;
},
editActive: function(e) {
e.preventDefault();
var x = this.$('.isactive').prop('checked'); // property of input checkbox
alert('click'+x);
var y = this.model.set('Active', x);
}
jquery文档“使用.empty()。html(字符串)而不是.html(字符串),以便在将新字符串分配给元素之前从文档中删除元素。”实际上没有预期的功能 –