[Pkg-javascript-commits] [backbone] 137/281: allowing view events to be functions instead of simply names of properties on a view

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:05 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 1c158747a09c89711170e73fcaefac8a76a0e5cf
Author: Zack Owens <ztowens at indiana.edu>
Date:   Sun Dec 25 00:03:56 2011 -0500

    allowing view events to be functions instead of simply names of properties on a view
---
 backbone.js  |  4 +++-
 test/view.js | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/backbone.js b/backbone.js
index a74453b..e85eb52 100644
--- a/backbone.js
+++ b/backbone.js
@@ -930,6 +930,7 @@
     //     {
     //       'mousedown .title':  'edit',
     //       'click .button':     'save'
+    //       'click a': function (evnt) { ... }
     //     }
     //
     // pairs. Callbacks will be bound to the view, with `this` set properly.
@@ -941,7 +942,8 @@
       if (!(events || (events = getValue(this, 'events')))) return;
       this.undelegateEvents();
       for (var key in events) {
-        var method = this[events[key]];
+        var method = events[key];
+        if (!_.isFunction(method)) method = this[events[key]];
         if (!method) throw new Error('Event "' + events[key] + '" does not exist');
         var match = key.match(eventSplitter);
         var eventName = match[1], selector = match[2];
diff --git a/test/view.js b/test/view.js
index 152f1d4..97fe632 100644
--- a/test/view.js
+++ b/test/view.js
@@ -56,6 +56,20 @@ $(document).ready(function() {
     equals(counter2, 3);
   });
 
+  test("View: delegateEvents allows functions for callbacks", function() {
+    var counter = 0;
+    view.el = "#qunit-banner";
+    var events = {"click": function() { counter++; }};
+    view.delegateEvents(events);
+    $('#qunit-banner').trigger('click');
+    equals(counter, 1);
+    $('#qunit-banner').trigger('click');
+    equals(counter, 2);
+    view.delegateEvents(events);
+    $('#qunit-banner').trigger('click');
+    equals(counter, 3);
+  });
+
   test("View: undelegateEvents", function() {
     var counter = counter2 = 0;
     view.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