diff --git a/backbone.js b/backbone.js index 01db0c28..78033d41 100644 --- a/backbone.js +++ b/backbone.js @@ -1183,6 +1183,13 @@ return this; }, + // **destroy** should clean up any references created by this view, + // preventing memory leaks. The convention is for **destroy** to always + // return `this`. + destroy: function() { + return this; + }, + // Remove this view from the DOM. Note that the view isn't present in the // DOM by default, so calling this method may be a no-op. remove: function() { diff --git a/index.html b/index.html index ff07ce82..06cde8d2 100644 --- a/index.html +++ b/index.html @@ -385,6 +385,7 @@
  • $ (jQuery or Zepto)
  • render
  • remove
  • +
  • destroy
  • make
  • delegateEvents
  • undelegateEvents
  • @@ -2337,6 +2338,26 @@ var Bookmark = Backbone.View.extend({ view.$el.remove();

    +

    + destroyview.destroy() +
    + The default implementation of destroy is a no-op. It should be + overridden in order to clean up any references created by a view, + either to itself or other objects, in order to prevent memory leaks. + By convention, destroy should return this for + chainability. +

    + +
    +var View = Backbone.View.extend({
    +  destroy: function() {
    +    if (this.model) this.model.off(null, null, this);
    +    if (this.collection) this.collection.off(null, null, this);
    +    return this;
    +  }
    +});
    +
    +

    makeview.make(tagName, [attributes], [content])