fix(jqLite): triggerHandler support unbind self

Fixes .one if the event is invoked from triggerHandler.

Closes #5984
This commit is contained in:
Chris Chua
2014-01-25 11:43:16 -08:00
committed by Igor Minar
parent f3a763fd2e
commit 8a27abae89
2 changed files with 24 additions and 2 deletions

View File

@@ -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 || [];
@@ -962,7 +964,7 @@ forEach({
stopPropagation: noop
}];
forEach(eventFns, function(fn) {
forEach(eventFnsCopy, function(fn) {
fn.apply(element, event.concat(eventData));
});
}

View File

@@ -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);
});
});