mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
fix(jqLite): support unbind self within handler
If an event handler unbinds itself, the next event handler on the same event and element doesn't get executed. This works fine in jQuery, and since jqLite doesn't support .one, this might be a common use case.
This commit is contained in:
parent
d5c5e2b584
commit
2f91cfd0d2
2 changed files with 24 additions and 1 deletions
|
|
@ -645,7 +645,10 @@ function createEventHandler(element, events) {
|
|||
return event.defaultPrevented || event.returnValue === false;
|
||||
};
|
||||
|
||||
forEach(events[type || event.type], function(fn) {
|
||||
// Copy event handlers in case event handlers array is modified during execution.
|
||||
var eventHandlersCopy = shallowCopy(events[type || event.type] || []);
|
||||
|
||||
forEach(eventHandlersCopy, function(fn) {
|
||||
fn.call(element, event);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1120,6 +1120,26 @@ describe('jqLite', function() {
|
|||
});
|
||||
|
||||
|
||||
it('should deregister specific listener within the listener and call subsequent listeners', function() {
|
||||
var aElem = jqLite(a),
|
||||
clickSpy = jasmine.createSpy('click'),
|
||||
clickOnceSpy = jasmine.createSpy('clickOnce').andCallFake(function() {
|
||||
aElem.off('click', clickOnceSpy);
|
||||
});
|
||||
|
||||
aElem.on('click', clickOnceSpy);
|
||||
aElem.on('click', clickSpy);
|
||||
|
||||
browserTrigger(a, 'click');
|
||||
expect(clickOnceSpy).toHaveBeenCalledOnce();
|
||||
expect(clickSpy).toHaveBeenCalledOnce();
|
||||
|
||||
browserTrigger(a, 'click');
|
||||
expect(clickOnceSpy).toHaveBeenCalledOnce();
|
||||
expect(clickSpy.callCount).toBe(2);
|
||||
});
|
||||
|
||||
|
||||
it('should deregister specific listener for multiple types separated by spaces', function() {
|
||||
var aElem = jqLite(a),
|
||||
masterSpy = jasmine.createSpy('master'),
|
||||
|
|
|
|||
Loading…
Reference in a new issue