From 4b2ce42180d73206ff77775b8dd80c7f189f3559 Mon Sep 17 00:00:00 2001 From: Tim Griesser Date: Mon, 24 Dec 2012 15:00:50 -0500 Subject: [PATCH] removing context from listenTo/stoplistening --- backbone.js | 12 +++++------- index.html | 6 +++--- test/events.js | 24 ------------------------ 3 files changed, 8 insertions(+), 34 deletions(-) diff --git a/backbone.js b/backbone.js index 870b1b9e..f36dde79 100644 --- a/backbone.js +++ b/backbone.js @@ -190,27 +190,25 @@ // An inversion-of-control version of `on`. Tell *this* object to listen to // an event in another object ... keeping track of what it's listening to. - listenTo: function(object, events, callback, context) { - context = context || this; + listenTo: function(object, events, callback) { var listeners = this._listeners || (this._listeners = {}); var id = object._listenerId || (object._listenerId = _.uniqueId('l')); listeners[id] = object; - object.on(events, callback || context, context); + object.on(events, callback || this, this); return this; }, // Tell this object to stop listening to either specific events ... or // to every object it's currently listening to. - stopListening: function(object, events, callback, context) { - context = context || this; + stopListening: function(object, events, callback) { var listeners = this._listeners; if (!listeners) return; if (object) { - object.off(events, callback, context); + object.off(events, callback, this); if (!events && !callback) delete listeners[object._listenerId]; } else { for (var id in listeners) { - listeners[id].off(null, null, context); + listeners[id].off(null, null, this); } this._listeners = {}; } diff --git a/index.html b/index.html index 947409ed..845b2843 100644 --- a/index.html +++ b/index.html @@ -773,10 +773,10 @@ object.off();

- listenToobject.listenTo(other, event, callback, [context]) + listenToobject.listenTo(other, event, callback)
Tell an object to listen to a particular event on an other object. - The advantage of using this form, instead of other.on(event, callback, [context]), + The advantage of using this form, instead of other.on(event, callback), is that listenTo allows the object to keep track of the events, and they can be removed all at once later on.

@@ -786,7 +786,7 @@ view.listenTo(model, 'change', view.render);

- stopListeningobject.stopListening([other], [event], [callback], [context]) + stopListeningobject.stopListening([other], [event], [callback])
Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove diff --git a/test/events.js b/test/events.js index 93af6acd..cf6c8f53 100644 --- a/test/events.js +++ b/test/events.js @@ -86,30 +86,6 @@ $(document).ready(function() { b.trigger('change'); }); - test("listenTo with context", 1, function() { - var a = _.extend({}, Backbone.Events); - var ctx = {}; - a.listenTo(a, 'foo', function(){ equal(this, ctx); }, ctx); - a.trigger('foo'); - }); - - test("stopListening with context", 2, function() { - var a = _.extend({}, Backbone.Events); - var ctx = {}; - var calledWithContext = false; - var calledWithoutContext = false; - - a.listenTo(a, 'foo', function(){ calledWithContext = true; }, ctx); - a.listenTo(a, 'foo', function(){ calledWithoutContext = true; }); - - a.stopListening(a, 'foo', null, ctx); - - a.trigger('foo'); - - equal(false, calledWithContext); - equal(true, calledWithoutContext); - }); - test("trigger all for each event", 3, function() { var a, b, obj = { counter: 0 }; _.extend(obj, Backbone.Events);