Destroy or remove a view in Backbone.js Destroy or remove a view in Backbone.js javascript javascript

Destroy or remove a view in Backbone.js


I had to be absolutely sure the view was not just removed from DOM but also completely unbound from events.

destroy_view: function() {    // COMPLETELY UNBIND THE VIEW    this.undelegateEvents();    this.$el.removeData().unbind();     // Remove view from DOM    this.remove();      Backbone.View.prototype.remove.call(this);}

Seemed like overkill to me, but other approaches did not completely do the trick.


Without knowing all the information... You could bind a reset trigger to your model or controller:

this.bind("reset", this.updateView);

and when you want to reset the views, trigger a reset.

For your callback, do something like:

updateView: function() {  view.remove();  view.render();};


I know I am late to the party, but hopefully this will be useful for someone else. If you are using backbone v0.9.9+, you could use, listenTo and stopListening

initialize: function () {    this.listenTo(this.model, 'change', this.render);    this.listenTo(this.model, 'destroy', this.remove);}

stopListening is called automatically by remove. You can read more here and here