I have a top level PageView that will re-render itself whenever the route changes. I have many nested sub-views embedded into this PageView. If I was to re-render PageView, do
A simple and modular class you might find useful.
ContainerView = Backbone.View.extend({
initialize: function() {
this.children = [];
},
remove: function() {
Backbone.View.prototype.remove.apply(this, arguments);
this.removeAllChildren();
},
removeAllChildren: function() {
_.each(this.children, function(view) { view.remove(); });
this.children = [];
},
appendAllChildren: function() {
_.each(this.children, function(view) { this.$el.append(view.render().$el); }, this);
}
});
usage:
MyView = ContainerView.extend({
render: function() {
this.removeAllChildren();
this.$el.empty();
// For each child view...
// this.children.push(new SomeControl(...));
this.appendAllChildren();
return this;
}
});