First of all, you don't need your dispose
method, the standard remove
is sufficient:
var attrView = new AttributeView();
//....
attrView.remove(); // <--------- Do this instead
//...
attrView = new AttributeView()
attrView.render();
Secondly, you can override remove
if the standard version doesn't do what you need. The default implementation simply removes this.el
and cleans up some event listeners:
remove: function() {
this.$el.remove();
this.stopListening();
return this;
},
In your case, you want to undo everything that render
does and that means clearing out the HTML inside this.el
and removing the events by calling undelegateEvents
:
remove: function() {
this.undelegateEvents();
this.$el.empty();
this.stopListening();
return this;
},
Then you can call attrView.remove()
and kill it off and (new AttributeView()).render()
to bring it back.
Demo: http://jsfiddle.net/ambiguous/Pu9a2/3/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…