Since Backbone.js is quite flexible, I am wondering which approach is best for certain things. Here I am wondering if I should build my applications so that ".render ()" and ".remove ()" correctly change each other.
First, the path that seems the cleanest is to pass the view to an identifier or jQuery element to join. If everything is done like this, but calling ".render ()" incorrectly replaces the view in the DOM, since the main element never returns to the DOM:
App.ChromeView = Backbone.View.extend({
render: function() {
this.sidebar = new App.SidebarView({ el: this.$(".sidebar") });
this.menu = new App.NavigationView({ el: this.$("nav") });
}
});
$(function() {
App.chrome = new App.ChromeView({ el: $("#chrome") });
});
It seems to me preferable to tweak it so that .remove () and .render () are exact opposites:
App.ChromeView = Backbone.View.extend({
render: function() {
this.$el.appendTo('body');
this.sidebar = new App.SidebarView({ el: this.$(".sidebar") });
this.menu = new App.NavigationView({ el: this.$("nav") });
}
});
$(function() {
App.chrome = new App.ChromeView();
});
Backbone.js? .remove() .render() ?