Skip to content

Commit

Permalink
Merge pull request #858 from braddunbar/trigger-multiple
Browse files Browse the repository at this point in the history
trigger all for each event
  • Loading branch information
jashkenas committed Jan 14, 2012
2 parents 9c0e7f0 + 9df6387 commit 61fc127
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
18 changes: 9 additions & 9 deletions backbone.js
Expand Up @@ -116,22 +116,22 @@
// 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(evs) {
var event, node, calls, tail, args;
var events = evs.split(/\s+/);
events.unshift('all');
events.push(null);
trigger : function(events) {
var event, node, calls, tail, args, all, rest;
if (!(calls = this._callbacks)) return this;
// Save references to the current head, tail, & args.
all = calls['all'];
(events = events.split(/\s+/)).push(null);
// Save references to the current heads & tails.
while (event = events.shift()) {
if (all) events.push({next: all.next, tail: all.tail, event: event});
if (!(node = calls[event])) continue;
args = event == 'all' ? arguments : slice.call(arguments, 1);
events.push({next: node.next, tail: node.tail, args: args});
events.push({next: node.next, tail: node.tail});
}
// Traverse each list, stopping when the saved tail is reached.
rest = slice.call(arguments, 1);
while (node = events.pop()) {
tail = node.tail;
args = node.args;
args = node.event ? [node.event].concat(rest) : rest;
while ((node = node.next) !== tail) {
node.callback.apply(node.context || this, args);
}
Expand Down
14 changes: 14 additions & 0 deletions test/events.js
Expand Up @@ -35,6 +35,20 @@ $(document).ready(function() {
equals(obj.counter, 5);
});

test("Events: trigger all for each event", function() {
var a, b, obj = { counter: 0 };
_.extend(obj, Backbone.Events);
obj.on('all', function(event) {
obj.counter++;
if (event == 'a') a = true;
if (event == 'b') b = true;
})
.trigger('a b');
ok(a);
ok(b);
equal(obj.counter, 2);
});

test("Events: on, then unbind all functions", function() {
var obj = { counter: 0 };
_.extend(obj,Backbone.Events);
Expand Down

0 comments on commit 61fc127

Please sign in to comment.