[Pkg-javascript-commits] [backbone] 27/281: Clear event callbacks with clearEvents

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:01:53 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 1aaffa1302e2646154dfc5b10fa1c918907d8fe3
Author: Les Hill <leshill at gmail.com>
Date:   Sun Sep 18 23:29:20 2011 -0700

    Clear event callbacks with clearEvents
---
 backbone.js  |  7 ++++++-
 test/view.js | 21 +++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/backbone.js b/backbone.js
index 54efe48..f5ade49 100644
--- a/backbone.js
+++ b/backbone.js
@@ -954,7 +954,7 @@
     delegateEvents : function(events) {
       if (!(events || (events = this.events))) return;
       if (_.isFunction(events)) events = events.call(this);
-      $(this.el).unbind('.delegateEvents' + this.cid);
+      this.clearEvents();
       for (var key in events) {
         var method = this[events[key]];
         if (!method) throw new Error('Event "' + events[key] + '" does not exist');
@@ -970,6 +970,11 @@
       }
     },
 
+    // Clears all callbacks previously bound to the view with `delegateEvents`.
+    clearEvents: function() {
+      $(this.el).unbind('.delegateEvents' + this.cid);
+    },
+
     // Performs the initial configuration of a View with a set of options.
     // Keys with special meaning *(model, collection, id, className)*, are
     // attached directly to the view.
diff --git a/test/view.js b/test/view.js
index 1f007c2..88bc4b1 100644
--- a/test/view.js
+++ b/test/view.js
@@ -56,6 +56,27 @@ $(document).ready(function() {
     equals(counter2, 3);
   });
 
+  test("View: clearEvents", function() {
+    var counter = counter2 = 0;
+    view.el = document.body;
+    view.increment = function(){ counter++; };
+    $(view.el).unbind('click');
+    $(view.el).bind('click', function(){ counter2++; });
+    var events = {"click #qunit-userAgent": "increment"};
+    view.delegateEvents(events);
+    $('#qunit-userAgent').trigger('click');
+    equals(counter, 1);
+    equals(counter2, 1);
+    view.clearEvents();
+    $('#qunit-userAgent').trigger('click');
+    equals(counter, 1);
+    equals(counter2, 2);
+    view.delegateEvents(events);
+    $('#qunit-userAgent').trigger('click');
+    equals(counter, 2);
+    equals(counter2, 3);
+  });
+
   test("View: _ensureElement with DOM node el", function() {
     var ViewClass = Backbone.View.extend({
       el: document.body

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