Skip to content

Commit

Permalink
Merge pull request #2019 from tgriesser/nocontext
Browse files Browse the repository at this point in the history
Removing context from listenTo/stoplistening
  • Loading branch information
jashkenas committed Dec 28, 2012
2 parents 678bdd0 + 4b2ce42 commit d63fe9e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 34 deletions.
12 changes: 5 additions & 7 deletions backbone.js
Expand Up @@ -190,27 +190,25 @@


// An inversion-of-control version of `on`. Tell *this* object to listen to // 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. // an event in another object ... keeping track of what it's listening to.
listenTo: function(object, events, callback, context) { listenTo: function(object, events, callback) {
context = context || this;
var listeners = this._listeners || (this._listeners = {}); var listeners = this._listeners || (this._listeners = {});
var id = object._listenerId || (object._listenerId = _.uniqueId('l')); var id = object._listenerId || (object._listenerId = _.uniqueId('l'));
listeners[id] = object; listeners[id] = object;
object.on(events, callback || context, context); object.on(events, callback || this, this);
return this; return this;
}, },


// Tell this object to stop listening to either specific events ... or // Tell this object to stop listening to either specific events ... or
// to every object it's currently listening to. // to every object it's currently listening to.
stopListening: function(object, events, callback, context) { stopListening: function(object, events, callback) {
context = context || this;
var listeners = this._listeners; var listeners = this._listeners;
if (!listeners) return; if (!listeners) return;
if (object) { if (object) {
object.off(events, callback, context); object.off(events, callback, this);
if (!events && !callback) delete listeners[object._listenerId]; if (!events && !callback) delete listeners[object._listenerId];
} else { } else {
for (var id in listeners) { for (var id in listeners) {
listeners[id].off(null, null, context); listeners[id].off(null, null, this);
} }
this._listeners = {}; this._listeners = {};
} }
Expand Down
6 changes: 3 additions & 3 deletions index.html
Expand Up @@ -772,10 +772,10 @@ <h2 id="Events">Backbone.Events</h2>
</p> </p>


<p id="Events-listenTo"> <p id="Events-listenTo">
<b class="header">listenTo</b><code>object.listenTo(other, event, callback, [context])</code> <b class="header">listenTo</b><code>object.listenTo(other, event, callback)</code>
<br /> <br />
Tell an <b>object</b> to listen to a particular event on an <b>other</b> object. Tell an <b>object</b> to listen to a particular event on an <b>other</b> object.
The advantage of using this form, instead of <tt>other.on(event, callback, [context])</tt>, The advantage of using this form, instead of <tt>other.on(event, callback)</tt>,
is that <b>listenTo</b> allows the <b>object</b> to keep track of the events, is that <b>listenTo</b> allows the <b>object</b> to keep track of the events,
and they can be removed all at once later on. and they can be removed all at once later on.
</p> </p>
Expand All @@ -785,7 +785,7 @@ <h2 id="Events">Backbone.Events</h2>
</pre> </pre>


<p id="Events-stopListening"> <p id="Events-stopListening">
<b class="header">stopListening</b><code>object.stopListening([other], [event], [callback], [context])</code> <b class="header">stopListening</b><code>object.stopListening([other], [event], [callback])</code>
<br /> <br />
Tell an <b>object</b> to stop listening to events. Either call Tell an <b>object</b> to stop listening to events. Either call
<b>stopListening</b> with no arguments to have the <b>object</b> remove <b>stopListening</b> with no arguments to have the <b>object</b> remove
Expand Down
24 changes: 0 additions & 24 deletions test/events.js
Expand Up @@ -86,30 +86,6 @@ $(document).ready(function() {
b.trigger('change'); 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() { test("trigger all for each event", 3, function() {
var a, b, obj = { counter: 0 }; var a, b, obj = { counter: 0 };
_.extend(obj, Backbone.Events); _.extend(obj, Backbone.Events);
Expand Down

0 comments on commit d63fe9e

Please sign in to comment.