Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Fixes jashkenas#640 -- adds the ability to bind/unbind/trigger (on/of…
Browse files Browse the repository at this point in the history
…f/trigger) multiple, space separated events, after jQuery.
  • Loading branch information
jashkenas committed Jan 13, 2012
1 parent 51335bf commit 5aa4fda
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
49 changes: 29 additions & 20 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,38 @@

// Bind an event, specified by a string name, `ev`, to a `callback`
// function. Passing `"all"` will bind the callback to all events fired.
on : function(ev, callback, context) {
var calls = this._callbacks || (this._callbacks = {});
var list = calls[ev] || (calls[ev] = {});
var tail = list.tail || (list.tail = list.next = {});
tail.callback = callback;
tail.context = context;
list.tail = tail.next = {};
on : function(events, callback, context) {
var ev;
events = events.split(/\s+/);
while (ev = events.shift()) {
var calls = this._callbacks || (this._callbacks = {});
var list = calls[ev] || (calls[ev] = {});
var tail = list.tail || (list.tail = list.next = {});
tail.callback = callback;
tail.context = context;
list.tail = tail.next = {};
}
return this;
},

// Remove one or many callbacks. If `context` is null, removes all callbacks
// with that function. If `callback` is null, removes all callbacks for the
// event. If `ev` is null, removes all bound callbacks for all events.
off : function(ev, callback, context) {
var calls, node;
if (!ev) {
off : function(events, callback, context) {
var ev, calls, node;
if (!events) {
delete this._callbacks;
} else if (calls = this._callbacks) {
node = calls[ev];
delete calls[ev];
if (!callback || !node) return this;
while ((node = node.next) && node.next) {
if (node.callback === callback &&
(!context || node.context === context)) continue;
this.on(ev, node.callback, node.context);
events = events.split(/\s+/);
while (ev = events.shift()) {
node = calls[ev];
delete calls[ev];
if (!callback || !node) continue;
while ((node = node.next) && node.next) {
if (node.callback === callback &&
(!context || node.context === context)) continue;
this.on(ev, node.callback, node.context);
}
}
}
return this;
Expand All @@ -105,9 +112,11 @@
// Trigger an event, firing all bound callbacks. Callbacks are passed the
// same arguments as `trigger` is, apart from the event name.
// Listening for `"all"` passes the true event name as the first argument.
trigger : function(ev) {
var node, calls, tail, args, event;
var events = ['all', ev, null];
trigger : function(evs) {
var event, node, calls, tail, args;
var events = evs.split(/\s+/);
events.unshift('all');
events.push(null);
if (!(calls = this._callbacks)) return this;
while (event = events.shift()) {
if (!(node = calls[event])) continue;
Expand Down
26 changes: 24 additions & 2 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ $(document).ready(function() {
equals(obj.counter, 5, 'counter should be incremented five times.');
});

test("Events: binding and triggering multiple events", function() {
var obj = { counter: 0 };
_.extend(obj,Backbone.Events);

obj.on('a b c', function() { obj.counter += 1; });

obj.trigger('a');
equals(obj.counter, 1);

obj.trigger('a b');
equals(obj.counter, 3);

obj.trigger('c');
equals(obj.counter, 4);

obj.off('a c');
obj.trigger('a b c');
equals(obj.counter, 5);
});

test("Events: on, then unbind all functions", function() {
var obj = { counter: 0 };
_.extend(obj,Backbone.Events);
Expand Down Expand Up @@ -70,9 +90,11 @@ $(document).ready(function() {
test("Events: bind a callback with a supplied context", function () {
expect(1);

var TestClass = function () { return this; }
var TestClass = function () {
return this;
};
TestClass.prototype.assertTrue = function () {
ok(true, '`this` was bound to the callback')
ok(true, '`this` was bound to the callback');
};

var obj = _.extend({},Backbone.Events);
Expand Down

0 comments on commit 5aa4fda

Please sign in to comment.