I am creating a single page application, and I am quite new to backbone. I have a problem with creating multiple views which uses the same wrapper-div.
My setup:
I have added a close function to all views:
Backbone.View.prototype.close = function(){
this.remove();
this.off();
if (this.onClose){
this.onClose();
}
}
I have a wrapper-div where I want to render views, remove them and render new ones. So my SetupView looks like this:
app.SetupView = Backbone.View.extend({
el: '#my_view_wrapper',
...
});
From the function where I swap views I close the current (open) view like this:
var v = this.model.get('view');
v.close();
Question
My problem is that I have multiple view's using the same wrapper-div. But when I close a view, this wrapper-div seems to be removed, and the next view I try to create can't find this div.
I guess there is an easy solution? I want to reuse the same wrapper, and only remove the view inside it, not the wrapper itself.
remove
removes the element from the DOM. But I think I misunderstoodel
. I thought it just was the place the new element got injected, but have now learned that it becomes a part of the new element, and therefor it is removed when i callremove
. – swenedoel
: the view should create itsel
, do things to it, and remove it when the view is removed. The caller puts the view'sel
inside a container that the caller controls. Not re-using DOM elements for multiple views helps you avoid a lot of problems. – mu is too short