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

Conflicts:
	test/jqLiteSpec.js
  • Loading branch information
chrisirhc authored and IgorMinar committed Jul 24, 2014
1 parent ceaca57 commit 209e600
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 @@ -953,7 +953,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 @@ -1660,6 +1660,26 @@ describe('jqLite', function() {
data = pokeSpy.mostRecentCall.args[1];
expect(data.hello).toBe("world");
});


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 209e600

Please sign in to comment.