From 4a20d66f6675b6bc73d1109cedf261012512bd9e Mon Sep 17 00:00:00 2001 From: Radoslav Stankov Date: Fri, 5 Oct 2012 22:04:12 +0300 Subject: [PATCH] Check if model/collection support .off when disposing of the view --- backbone.js | 4 ++-- test/view.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/backbone.js b/backbone.js index 3457b0f4..d8e31b35 100644 --- a/backbone.js +++ b/backbone.js @@ -1234,8 +1234,8 @@ // memory leaks. dispose: function() { this.undelegateEvents(); - if (this.model) this.model.off(null, null, this); - if (this.collection) this.collection.off(null, null, this); + if (this.model && this.model.off) this.model.off(null, null, this); + if (this.collection && this.collection.off) this.collection.off(null, null, this); return this; }, diff --git a/test/view.js b/test/view.js index d2741a74..1de5c20e 100644 --- a/test/view.js +++ b/test/view.js @@ -286,11 +286,11 @@ $(document).ready(function() { test("dispose", 0, function() { var View = Backbone.View.extend({ events: { - click: function() { ok(false); } + click: function() { fail(); } }, initialize: function() { - this.model.on('all x', function(){ ok(false); }, this); - this.collection.on('all x', function(){ ok(false); }, this); + this.model.on('all x', function(){ fail(); }, this); + this.collection.on('all x', function(){ fail(); }, this); } }); @@ -305,6 +305,11 @@ $(document).ready(function() { view.$el.click(); }); + test("dispose with non Backbone objects", 0, function() { + var view = new Backbone.View({model: {}, collection: {}}); + view.dispose(); + }); + test("view#remove calls dispose.", 1, function() { var view = new Backbone.View();