[Pkg-javascript-commits] [backbone] 195/281: Fixes #640 -- adds the ability to bind/unbind/trigger (on/off/trigger) multiple, space separated events, after jQuery.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:12 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 5aa4fda9ba9db25c833b7243860de99b4af21073
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Fri Jan 13 16:01:59 2012 -0500

    Fixes #640 -- adds the ability to bind/unbind/trigger (on/off/trigger) multiple, space separated events, after jQuery.
---
 backbone.js    | 49 +++++++++++++++++++++++++++++--------------------
 test/events.js | 26 ++++++++++++++++++++++++--
 2 files changed, 53 insertions(+), 22 deletions(-)

diff --git a/backbone.js b/backbone.js
index 1a77c4d..965c5ba 100644
--- a/backbone.js
+++ b/backbone.js
@@ -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;
@@ -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;
diff --git a/test/events.js b/test/events.js
index 2c77d02..5997c13 100644
--- a/test/events.js
+++ b/test/events.js
@@ -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);
@@ -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);

-- 
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