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

Commit

Permalink
fix(jqLite): support unbind self within handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chrisirhc authored and vojtajina committed Dec 17, 2013
1 parent d5c5e2b commit 2f91cfd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
20 changes: 20 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 2f91cfd

Please sign in to comment.