[Pkg-javascript-commits] [backbone] 204/281: trigger all for each event

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:13 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.9.0
in repository backbone.

commit 9df6387d9eee705c303fd237b23b4cd90e90b9c3
Author: Brad Dunbar <dunbarb2 at gmail.com>
Date:   Fri Jan 13 18:58:11 2012 -0500

    trigger all for each event
---
 backbone.js    | 18 +++++++++---------
 test/events.js | 14 ++++++++++++++
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/backbone.js b/backbone.js
index 1d6dfd0..db86523 100644
--- a/backbone.js
+++ b/backbone.js
@@ -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);
         }
diff --git a/test/events.js b/test/events.js
index 5997c13..b8d184b 100644
--- a/test/events.js
+++ b/test/events.js
@@ -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);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list