fix(jqLite): triggerHandler support unbind self

Fixes .one if the event is invoked from triggerHandler.

Closes #5984

Conflicts:
	test/jqLiteSpec.js
This commit is contained in:
Chris Chua
2014-01-25 11:43:16 -08:00
committed by Igor Minar
parent ceaca57786
commit 209e600070
2 changed files with 24 additions and 2 deletions
+4 -2
View File
@@ -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 || [];
@@ -962,7 +964,7 @@ forEach({
stopPropagation: noop
}];
forEach(eventFns, function(fn) {
forEach(eventFnsCopy, function(fn) {
fn.apply(element, event.concat(eventData));
});
}
+20
View File
@@ -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);
});
});