I'm new to backbone and i'm trying to re-render the contents of a view. I've put up the code in jsfiddle ...
So when user clicks re-render how can i remove the contents in the dom and show only the new items ?
I'm new to backbone and i'm trying to re-render the contents of a view. I've put up the code in jsfiddle ...
So when user clicks re-render how can i remove the contents in the dom and show only the new items ?
The safest thing to do is to keep track of your
QuestionView
instances inside yourAppView
. Then you can callremove
on eachQuestionView
before adding the new ones; theremove
method is a:Views should provide their own
remove
implementation to unbind from non-DOM events and thus prevent zombies. The default simply removes the view'sel
from the DOM but if you do it right from the beginning, things will keep working nicely when your code inevitably changes.First adjust your
QuestionView
to have aremove
method to remove the event you've bound to the model:Then you'll need a couple adjustments to
AppView
to keep track of yourQuestionView
s:Demo: http://jsfiddle.net/ambiguous/FF9eG/
I've also updated your code to use
on
andoff
instead ofbind
andunbind
to match the new style. Newer versions of Backbone also have a cached version of$(this.el)
inthis.$el
so I've updated the code to use that as well.