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

Commit

Permalink
fix(jqLite): triggerHandler support unbind self
Browse files Browse the repository at this point in the history
Fixes .one if the event is invoked from triggerHandler.

Closes #5984
  • Loading branch information
chrisirhc authored and IgorMinar committed Jul 24, 2014
1 parent f3a763f commit 8a27aba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,9 @@ forEach({
clone: jqLiteClone,

triggerHandler: function(element, eventName, eventData) {
var eventFns = (jqLiteExpandoStore(element, 'events') || {})[eventName];
// Copy event handlers in case event handlers array is modified during execution.
var eventFns = (jqLiteExpandoStore(element, 'events') || {})[eventName],
eventFnsCopy = shallowCopy(eventFns || []);

eventData = eventData || [];

Expand All @@ -962,7 +964,7 @@ forEach({
stopPropagation: noop
}];

forEach(eventFns, function(fn) {
forEach(eventFnsCopy, function(fn) {
fn.apply(element, event.concat(eventData));
});
}
Expand Down
20 changes: 20 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,26 @@ describe('jqLite', function() {
event.preventDefault();
expect(event.isDefaultPrevented()).toBe(true);
});


it('should support handlers that deregister themselves', function() {
var element = jqLite('<a>poke</a>'),
clickSpy = jasmine.createSpy('click'),
clickOnceSpy = jasmine.createSpy('clickOnce').andCallFake(function() {
element.off('click', clickOnceSpy);
});

element.on('click', clickOnceSpy);
element.on('click', clickSpy);

element.triggerHandler('click');
expect(clickOnceSpy).toHaveBeenCalledOnce();
expect(clickSpy).toHaveBeenCalledOnce();

element.triggerHandler('click');
expect(clickOnceSpy).toHaveBeenCalledOnce();
expect(clickSpy.callCount).toBe(2);
});
});


Expand Down

0 comments on commit 8a27aba

Please sign in to comment.