Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9ee9ca1

Browse files
vojtajinaIgorMinar
authored andcommitted
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 !
1 parent bb39d34 commit 9ee9ca1

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/jqLite.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,10 @@ forEach({
351351
dealoc: JQLiteDealoc,
352352

353353
bind: function(element, type, fn){
354-
var bind = JQLiteData(element, 'bind'),
355-
eventHandler;
354+
var bind = JQLiteData(element, 'bind');
356355
if (!bind) JQLiteData(element, 'bind', bind = {});
357356
forEach(type.split(' '), function(type){
358-
eventHandler = bind[type];
357+
var eventHandler = bind[type];
359358
if (!eventHandler) {
360359
bind[type] = eventHandler = function(event) {
361360
if (!event.preventDefault) {

test/jqLiteSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,23 @@ describe('jqLite', function(){
331331
browserTrigger(b, 'click');
332332
expect(log).toEqual('click on: A;click on: B;');
333333
});
334+
335+
it('should bind to all events separated by space', function() {
336+
var elm = jqLite(a),
337+
callback = jasmine.createSpy('callback');
338+
339+
elm.bind('click keypress', callback);
340+
elm.bind('click', callback);
341+
342+
browserTrigger(a, 'click');
343+
expect(callback).toHaveBeenCalled();
344+
expect(callback.callCount).toBe(2);
345+
346+
callback.reset();
347+
browserTrigger(a, 'keypress');
348+
expect(callback).toHaveBeenCalled();
349+
expect(callback.callCount).toBe(1);
350+
});
334351
});
335352

336353

0 commit comments

Comments
 (0)