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

Commit

Permalink
fix:jqLite: Fix binding to more events separated by space
Browse files Browse the repository at this point in the history
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 !
  • Loading branch information
vojtajina authored and IgorMinar committed Jul 13, 2011
1 parent bb39d34 commit 9ee9ca1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});


Expand Down

0 comments on commit 9ee9ca1

Please sign in to comment.