I got following example Backbone.View
:
var View = Backbone.View.extend({
tagName: "div",
className: "advertisement",
initialize: function () {
this.on('switch', this.
switch, this);
this.views = {};
this.views.laptop = new LaptopAdView();
this.views.piano = new PianoAdView();
this.views.food = new FoodAdView();
},
render: function () {
this.$el.empty();
this.
switch ('laptops');
return this;
},
switch: function (ad) {
var el;
if (ad === 'laptop') {
el = this.views.laptop.render().el;
} else if (ad === 'piano') {
el = this.views.piano.render().el;
} else {
el = this.views.food.render().el;
}
// reinsert the chosen view
this.$el.empty().append(el);
// bind all events new
this.delegateEvents();
}
});
As you see I use this.delegatEvents()
to recreate all event bindings. This is actually not a perfect solution... It would be far better when I use this.$el.detach();
or an other method so I cache the whole object with its event instead of re-rendering and recreating all events.
Now with working fiddle: http://jsfiddle.net/RRXnK/66/