Problem:
Events that are set inside a view are not firing after the view is replaced with something else using $element.html(anotherView)
and then put back on the page using #element.html(theView)
.
Example:
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"');
}
});
The events work after this code executes:
// Create the new view:
var buggyView = new BuggyView({});
// Add the view to the page
$element.html(buggyView.el);
This code would happen later when the view is replaced on the page with something else:
$element.html(anotherView.el);
After the view is added back onto the page, the events no longer work:
$element.html(buggyView.el);