aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Chua2013-11-24 14:40:46 -0800
committerVojta Jina2013-12-16 16:39:13 -0800
commit2f91cfd0d2986899c38641100c1851b2f9d3888a (patch)
tree944561843e11e07bfd0d2986c5560539eabde3e0
parentd5c5e2b5848dcb312390549a92c94c76822a5f4f (diff)
downloadangular.js-2f91cfd0d2986899c38641100c1851b2f9d3888a.tar.bz2
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.
-rw-r--r--src/jqLite.js5
-rw-r--r--test/jqLiteSpec.js20
2 files changed, 24 insertions, 1 deletions
diff --git a/src/jqLite.js b/src/jqLite.js
index 8a45f966..c1b5b36e 100644
--- a/src/jqLite.js
+++ b/src/jqLite.js
@@ -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);
});
diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js
index c4f47dcd..3a8fd404 100644
--- a/test/jqLiteSpec.js
+++ b/test/jqLiteSpec.js
@@ -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'),