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

fix(jqLite): triggerHandler support listeners that unbind from the event #5984

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,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 @@ -872,7 +874,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 @@ -1503,6 +1503,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