问题:
视图内设置的事件不会在视图被替换为其他内容后触发$element.html(anotherView)
,然后使用#element.html(theView)
.
例子:
var BuggyView = Backbone.View.extend({
// Event works the at first, but not after the view is replaced, then added back
// onto the page
events:{
'click #some-element': 'on_click_some_element'
},
// This function gets called before the view is replaced by another view, but
// not after it is added back into the page the second time
on_click_some_element:function(event){
alert('User clicked "some element"');
}
});
执行此代码后,事件起作用:
// Create the new view:
var buggyView = new BuggyView({});
// Add the view to the page
$element.html(buggyView.el);
当页面上的视图被其他内容替换时,此代码将在稍后发生:
$element.html(anotherView.el);
将视图添加回页面后,事件不再起作用:
$element.html(buggyView.el);