From 74c674d682bec2a05fd318334c9d7d3293fc15d6 Mon Sep 17 00:00:00 2001 From: Derick Bailey Date: Mon, 17 Dec 2012 08:08:27 -0600 Subject: [PATCH 1/5] allow context as 4th parameter to Events#listenTo method --- backbone.js | 5 +++-- test/events.js | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/backbone.js b/backbone.js index 38cc9912..7f28fe76 100644 --- a/backbone.js +++ b/backbone.js @@ -190,11 +190,12 @@ // 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) { + listenTo: function(object, events, callback, context) { var listeners = this._listeners || (this._listeners = {}); var id = object._listenerId || (object._listenerId = _.uniqueId('l')); listeners[id] = object; - object.on(events, callback || this, this); + context = context || this; + object.on(events, callback || this, context); return this; }, diff --git a/test/events.js b/test/events.js index c3f1d61d..bf409a8e 100644 --- a/test/events.js +++ b/test/events.js @@ -86,6 +86,13 @@ $(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(ctx, this); }, ctx); + a.trigger('foo'); + }); + test("trigger all for each event", 3, function() { var a, b, obj = { counter: 0 }; _.extend(obj, Backbone.Events); From 015e60dbb7e0cd0c27e66816d90eab35a9180165 Mon Sep 17 00:00:00 2001 From: Derick Bailey Date: Mon, 17 Dec 2012 08:15:28 -0600 Subject: [PATCH 2/5] allow context as 4th parameter of stopListening --- backbone.js | 9 +++++---- test/events.js | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/backbone.js b/backbone.js index 7f28fe76..6220f914 100644 --- a/backbone.js +++ b/backbone.js @@ -191,25 +191,26 @@ // 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; var listeners = this._listeners || (this._listeners = {}); var id = object._listenerId || (object._listenerId = _.uniqueId('l')); listeners[id] = object; - context = context || this; object.on(events, callback || this, context); 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) { + stopListening: function(object, events, callback, context) { + contxt = context || this; var listeners = this._listeners; if (!listeners) return; if (object) { - object.off(events, callback, this); + object.off(events, callback, context); if (!events && !callback) delete listeners[object._listenerId]; } else { for (var id in listeners) { - listeners[id].off(null, null, this); + listeners[id].off(null, null, context); } this._listeners = {}; } diff --git a/test/events.js b/test/events.js index bf409a8e..946c25bb 100644 --- a/test/events.js +++ b/test/events.js @@ -89,10 +89,27 @@ $(document).ready(function() { test("listenTo with context", 1, function() { var a = _.extend({}, Backbone.Events); var ctx = {}; - a.listenTo(a, 'foo', function(){ equal(ctx, this); }, 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); From d6dd3d8647e781cd556ddac4adc46ef177aadba8 Mon Sep 17 00:00:00 2001 From: Derick Bailey Date: Mon, 17 Dec 2012 08:17:03 -0600 Subject: [PATCH 3/5] updated docs to add context param in listenTo and stopListening --- index.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 13bac91a..470ab885 100644 --- a/index.html +++ b/index.html @@ -773,10 +773,10 @@ object.off();

- listenToobject.listenTo(other, event, callback) + listenToobject.listenTo(other, event, callback, [context])
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), + The advantage of using this form, instead of other.on(event, callback, [context]), 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]) + stopListeningobject.stopListening([other], [event], [callback], [context])
Tell an object to stop listening to events. Either call stopListening with no arguments to have the object remove From 90099afc3181f93e1154d6c26f5a703b2d0689d1 Mon Sep 17 00:00:00 2001 From: Derick Bailey Date: Mon, 17 Dec 2012 09:00:42 -0600 Subject: [PATCH 4/5] accounting for event maps --- backbone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index 6220f914..ab8c40ca 100644 --- a/backbone.js +++ b/backbone.js @@ -195,7 +195,7 @@ var listeners = this._listeners || (this._listeners = {}); var id = object._listenerId || (object._listenerId = _.uniqueId('l')); listeners[id] = object; - object.on(events, callback || this, context); + object.on(events, callback || context, context); return this; }, From 64aa208b5a9576c97739f6b7282a7fff1a429e9c Mon Sep 17 00:00:00 2001 From: Derick Bailey Date: Mon, 17 Dec 2012 09:01:32 -0600 Subject: [PATCH 5/5] fixed typo on context var in stopListening --- backbone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index ab8c40ca..236e101c 100644 --- a/backbone.js +++ b/backbone.js @@ -202,7 +202,7 @@ // 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) { - contxt = context || this; + context = context || this; var listeners = this._listeners; if (!listeners) return; if (object) {