trigger all for each event

This commit is contained in:
Brad Dunbar
2012-01-13 18:58:11 -05:00
parent 9c0e7f00ea
commit 9df6387d9e
2 changed files with 23 additions and 9 deletions

View File

@@ -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);
}

View File

@@ -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);