From 9ee9ca13da3883d06733637f9048a83d94e6f1f8 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Fri, 8 Jul 2011 01:55:47 +0200 Subject: [PATCH] fix:jqLite: Fix binding to more events separated by space The var eventHandler was defined outside forEach loop, so registering more events caused calling listeners registered by the last one. Regression: elm.bind('click keyup', callback1); elm.bind('click', callback2); elm.bind('keyup', callback3); Firing click event would have executed callback1, callback3 ! --- src/jqLite.js | 5 ++--- test/jqLiteSpec.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/jqLite.js b/src/jqLite.js index a1203739cf46..5e9d777a981e 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -351,11 +351,10 @@ forEach({ dealoc: JQLiteDealoc, bind: function(element, type, fn){ - var bind = JQLiteData(element, 'bind'), - eventHandler; + var bind = JQLiteData(element, 'bind'); if (!bind) JQLiteData(element, 'bind', bind = {}); forEach(type.split(' '), function(type){ - eventHandler = bind[type]; + var eventHandler = bind[type]; if (!eventHandler) { bind[type] = eventHandler = function(event) { if (!event.preventDefault) { diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 6794c4e08588..fafe7f2adf26 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -331,6 +331,23 @@ describe('jqLite', function(){ browserTrigger(b, 'click'); expect(log).toEqual('click on: A;click on: B;'); }); + + it('should bind to all events separated by space', function() { + var elm = jqLite(a), + callback = jasmine.createSpy('callback'); + + elm.bind('click keypress', callback); + elm.bind('click', callback); + + browserTrigger(a, 'click'); + expect(callback).toHaveBeenCalled(); + expect(callback.callCount).toBe(2); + + callback.reset(); + browserTrigger(a, 'keypress'); + expect(callback).toHaveBeenCalled(); + expect(callback.callCount).toBe(1); + }); });