I have two views, a MainView
and a ChildView
. The ChildView
is configured and instaciated outside of the MainView
instance and injected into the MainView
. The MainView
shows the ChildView
when it is rendered, thanks to the onRender
method.
The problem I am having is that if the MainView
is rendered a 2nd time, all event listeners on the ChildView
are killed. The ChildView
is still displayed on the 2nd render, however it doesn't respond to user events (like mouse clicks). Not even its own events.
var MainView = Marionette.Layout.extend({
regions: {
'menu': '.menu'
},
template: "#sample-template",
onRender: function() {
this.menu.show(this.options.menuView);
}
});
var ChildView = Marionette.ItemView.extend({
template: '#menu-template',
triggers: {
'click .js-click-me': 'clicked'
},
onClicked: function() {
alert('the button was clicked...');
}
});
// Where the view is used and displayed in a main app region:
var model = new Backbone.Model({
contentPlacement: "here"
});
var childView = new ChildView({
model: model
});
var view = new MainView({
model: model,
menuView: childView
});
this.region.show(view);
// A 2nd render kills event listeners in the childView
// Remove this line, and it will work again...
view.render();
I have a jsfiddle that demonstrates this. If you comment out the 2nd render
(line 65) you'll see that the events on the ChildView
do, in fact, work.
What is the best way to prevent this from happening? Is there a better way to configure and inject a view into another view in Marionette?