[Pkg-javascript-commits] [backbone] 184/281: (un)bind does not alter callback list during trigger

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:10 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 47e1f7e6c838a95e562dba9eafd39573b2dc9e04
Author: Brad Dunbar <dunbarb2 at gmail.com>
Date:   Sat Nov 5 12:03:29 2011 -0400

    (un)bind does not alter callback list during trigger
---
 backbone.js    | 41 ++++++++++++++++++++---------------------
 test/events.js | 14 ++++++++++++++
 2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/backbone.js b/backbone.js
index a1b228b..5beae96 100644
--- a/backbone.js
+++ b/backbone.js
@@ -86,20 +86,17 @@
     // with that function. If `callback` is null, removes all callbacks for the
     // event. If `ev` is null, removes all bound callbacks for all events.
     unbind : function(ev, callback, context) {
-      var calls, node, prev;
+      var calls, node;
       if (!ev) {
-        this._callbacks = null;
+        delete this._callbacks;
       } else if (calls = this._callbacks) {
-        if (!callback) {
-          calls[ev] = {};
-        } else if (node = calls[ev]) {
-          while ((prev = node) && (node = node.next)) {
-            if (node.callback !== callback) continue;
-            if (context && (context !== node.context)) continue;
-            prev.next = node.next;
-            node.context = node.callback = null;
-            // break;
-          }
+        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.bind(ev, node.callback, node.context);
         }
       }
       return this;
@@ -108,16 +105,18 @@
     // 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(eventName) {
-      var node, calls, callback, args, ev, events = ['all', eventName];
+    trigger : function(ev) {
+      var node, calls, tail, args, event, events = ['all', ev, null];
       if (!(calls = this._callbacks)) return this;
-      while (ev = events.pop()) {
-        if (!(node = calls[ev])) continue;
-        args = ev == 'all' ? arguments : slice.call(arguments, 1);
-        while (node = node.next) {
-          if (callback = node.callback) {
-            callback.apply(node.context || this, args);
-          }
+      while (event = events.shift()) {
+        if (!(node = calls[event])) continue;
+        args = event == 'all' ? arguments : slice.call(arguments, 1);
+        events.push({next: node.next, tail: node.tail, args: args});
+      }
+      while (node = events.pop()) {
+        tail = node.tail, args = node.args;
+        while ((node = node.next) !== tail) {
+          node.callback.apply(node.context || this, args);
         }
       }
       return this;
diff --git a/test/events.js b/test/events.js
index a8235ef..d4cde4c 100644
--- a/test/events.js
+++ b/test/events.js
@@ -95,4 +95,18 @@ $(document).ready(function() {
     equals(obj.counter, 3, 'counter should have been incremented three times');
   });
 
+  test("Events: callback list is not altered during trigger", function () {
+    var counter = 0, obj = _.extend({}, Backbone.Events);
+    var incr = function(){ counter++; };
+    obj.bind('event', function(){ obj.bind('event', incr).bind('all', incr); })
+    .trigger('event');
+    equals(counter, 0, 'bind does not alter callback list');
+    obj.unbind()
+    .bind('event', function(){ obj.unbind('event', incr).unbind('all', incr); })
+    .bind('event', incr)
+    .bind('all', incr)
+    .trigger('event');
+    equals(counter, 2, 'unbind does not alter callback list');
+  });
+
 });

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