[Pkg-javascript-commits] [backbone] 21/211: Multiple views may listen for events on the same element

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:58 UTC 2014


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

js pushed a commit to tag 0.5.0
in repository backbone.

commit 34d1d0ac936b23a04f921abc70f71ddcb90ca7d3
Author: Sam Stephenson <sam at 37signals.com>
Date:   Sat Dec 11 23:16:53 2010 -0600

    Multiple views may listen for events on the same element
---
 backbone.js  |  5 +++--
 test/view.js | 23 +++++++++++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/backbone.js b/backbone.js
index ec1ad2d..44310f6 100644
--- a/backbone.js
+++ b/backbone.js
@@ -772,6 +772,7 @@
   // Creating a Backbone.View creates its initial element outside of the DOM,
   // if an existing element is not provided...
   Backbone.View = function(options) {
+    this.vid = _.uniqueId('v');
     this._configure(options || {});
     this._ensureElement();
     this.delegateEvents();
@@ -843,13 +844,13 @@
     // not `change`, `submit`, and `reset` in Internet Explorer.
     delegateEvents : function(events) {
       if (!(events || (events = this.events))) return;
-      $(this.el).unbind('.delegateEvents');
+      $(this.el).unbind('.delegateEvents' + this.vid);
       for (var key in events) {
         var methodName = events[key];
         var match = key.match(eventSplitter);
         var eventName = match[1], selector = match[2];
         var method = _.bind(this[methodName], this);
-        eventName += '.delegateEvents';
+        eventName += '.delegateEvents' + this.vid;
         if (selector === '') {
           $(this.el).bind(eventName, method);
         } else {
diff --git a/test/view.js b/test/view.js
index 1fdfb71..60be9b8 100644
--- a/test/view.js
+++ b/test/view.js
@@ -64,4 +64,27 @@ $(document).ready(function() {
     equals(view.el, document.body);
   });
 
+  test("View: multiple views per element", function() {
+    var count = 0, ViewClass = Backbone.View.extend({
+      el: $("body"),
+      events: {
+        "click": "click"
+      },
+      click: function() {
+        count++;
+      }
+    });
+
+    var view1 = new ViewClass;
+    $("body").trigger("click");
+    equals(1, count);
+
+    var view2 = new ViewClass;
+    $("body").trigger("click");
+    equals(3, count);
+
+    view1.delegateEvents();
+    $("body").trigger("click");
+    equals(5, count);
+  });
 });

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