I am working on a backbone.js-application and have reached the point where I have a number of routers and views representing each part of my application. In the simplified router example below, I have two locations; account
& users
.
Both view in each location render their content to a mutual element, named #appcontainer
. My common sense says that I should make sure to remove
each view before launching another to prevent collisions in bindings, DOM and whatnot.
But as I cannot know for sure whether a view already has been created, I cannot explicitly call previousView.remove()
either from inside my router or views.
Would it be sufficient to add $(this.el).empty()
to the constructor of each view to clean out any eventual previous bindings and elements from the DOM?
Here's the router example?
var myRouter = Backbone.Router.extend({
routes: {
"account": "account",
"users": "users"
},
account: function() {
view = new AccountView({});
view.render();
},
users: function() {
view = new UserView({});
view.render();
}
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…