I keep reading about the Backbone.js zombie (or memory leak) problem. Basically you have to unbind and remove the element from the DOM when you no longer need it to make sure all events are removed as well.
Now, I have a single page app with a few containers:
<div id="page1"></div>
<div id="page2"></div>
and add my underscore.js templates to these placeholders. I have a model per page like:
HomeView = Backbone.View.extend({
el: '#page1'
)}
Now, when I click on an element on that page I navigate to another Backbone.js view:
clicked: function(ev){
$(this.el).remove(); // <-- this is the problem
$(this.el).unbind();
App.navigate('page/2', true);
}
This works fine but... I removed the page1
element from the DOM so when I use the back button to go to the previous page my element is gone and there is nothing to attach the HTML to.
I probably don't understand how to link Backbone.js views with the DOM... should I keep the element with the risk of memory leaks?
Thanks!