From dad7e2757ef54b91030ce47347e0b6805dead561 Mon Sep 17 00:00:00 2001
From: Brad Dunbar
Date: Mon, 28 May 2012 16:52:59 -0400
Subject: [PATCH 1/2] Fix #1339 - Add Backbone.View#destroy.
---
backbone.js | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/backbone.js b/backbone.js
index f1728acc..351e137c 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() {
From 767e7512784e9055c3c1f1bfe048053fc455d214 Mon Sep 17 00:00:00 2001
From: Brad Dunbar
Date: Sun, 3 Jun 2012 17:32:53 -0400
Subject: [PATCH 2/2] Documentation for View#destroy.
---
index.html | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
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();
+
+ view.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;
+ }
+});
+
+
view.make(tagName, [attributes], [content])