I’ve had a look at this question but I’m still having trouble maintaining the events of a subview after re-rendering. I tried to recreate my issue here:
var MainView = Backbone.View.extend({
initialize : function() {
this.composer = new Composer();
},
render : function() {
this.$el.html(this.composer.render().el);
}
});
var SubView = Backbone.View.extend({
tagName: 'li',
events: {
"click": "click"
},
click: function(){
console.log( "click!" );
},
render: function(){
this.$el.html( "click me" );
return this;
}
});
var Composer = Backbone.View.extend({
tagName : 'ul',
render: function(){
console.log( "Composer.render" );
this.$el.empty();
for (var i = 0; i < 5; i++) {
var sub = new SubView();
this.$el.append( sub.render().el );
}
sub.delegateEvents();
return this;
}
});
In the example re-rendering the view disables the click event in the subviews. I’m not sure why this doesn’t work.
Following the explanation from the link you posted. You just need to include a
this.composer.delegateEvents();call in between theempty()and theappend()calls. (Explained in the link)So simply modify your
MainViewas:Take a look at the JSFiddle here:
http://jsfiddle.net/4qrRa/5/