Merge pull request #1353 from braddunbar/destroy

Fix #1339 - Add Backbone.View#destroy.
This commit is contained in:
brad dunbar
2012-06-03 14:44:08 -07:00
2 changed files with 28 additions and 0 deletions

View File

@@ -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() {

View File

@@ -385,6 +385,7 @@
<li> <a href="#View-dollar">$ (jQuery or Zepto)</a></li>
<li> <a href="#View-render">render</a></li>
<li> <a href="#View-remove">remove</a></li>
<li> <a href="#View-destroy">destroy</a></li>
<li> <a href="#View-make">make</a></li>
<li> <a href="#View-delegateEvents">delegateEvents</a></li>
<li> <a href="#View-undelegateEvents">undelegateEvents</a></li>
@@ -2337,6 +2338,26 @@ var Bookmark = Backbone.View.extend({
<tt>view.$el.remove();</tt>
</p>
<p id="View-destroy">
<b class="header">destroy</b><code>view.destroy()</code>
<br />
The default implementation of <b>destroy</b> 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, <b>destroy</b> should <tt>return this</tt> for
chainability.
</p>
<pre>
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;
}
});
</pre>
<p id="View-make">
<b class="header">make</b><code>view.make(tagName, [attributes], [content])</code>
<br />