From c19827019a10ed2229b3901d54f9765b2532ab45 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 11 Sep 2013 15:39:54 -0300 Subject: [PATCH] Fixes #2724, leak with non-specific calls to stopListening. --- backbone.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/backbone.js b/backbone.js index 4b151da26..157edb7f3 100644 --- a/backbone.js +++ b/backbone.js @@ -155,11 +155,12 @@ var listeningTo = this._listeningTo; if (!listeningTo) return this; var remove = !name && !callback; - if (typeof name === 'object') callback = this; + if (!callback && typeof name === 'object') callback = this; if (obj) (listeningTo = {})[obj._listenId] = obj; for (var id in listeningTo) { - listeningTo[id].off(name, callback, this); - if (remove) delete this._listeningTo[id]; + obj = listeningTo[id]; + obj.off(name, callback, this); + if (remove || _.isEmpty(obj._events)) delete this._listeningTo[id]; } return this; } @@ -219,7 +220,7 @@ var listeningTo = this._listeningTo || (this._listeningTo = {}); var id = obj._listenId || (obj._listenId = _.uniqueId('l')); listeningTo[id] = obj; - if (typeof name === 'object') callback = this; + if (!callback && typeof name === 'object') callback = this; obj[implementation](name, callback, this); return this; };