Search code examples
javascriptbackbone.jsbackbone-events

Destroy or remove a view in Backbone.js


I'm currently trying to implement a destroy/remove method for views but I can't get a generic solution to work for all my views.

I was hoping there would be an event to attach to the controller, so that when a new request comes through it destroys previous views then loads the new ones.

Is there any way to do this without having to build a remove function for each view?


Solution

  • 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();
    };