diff options
| author | Vojta Jina | 2012-09-25 01:09:26 -0700 |
|---|---|---|
| committer | Igor Minar | 2012-11-25 11:39:54 +0100 |
| commit | e6966e05f508d1d2633b9ff327fea912b12555ac (patch) | |
| tree | eb7ff1052c75485a709e647cb8c550297746d573 /src | |
| parent | 682418f029cb9710a77eb377dd2a8390eaac4516 (diff) | |
| download | angular.js-e6966e05f508d1d2633b9ff327fea912b12555ac.tar.bz2 | |
fix(Scope): allow removing a listener during event
Diffstat (limited to 'src')
| -rw-r--r-- | src/ng/rootScope.js | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 5754ad32..de4b7407 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -638,7 +638,7 @@ function $RootScopeProvider(){ namedListeners.push(listener); return function() { - arrayRemove(namedListeners, listener); + namedListeners[indexOf(namedListeners, listener)] = null; }; }, @@ -686,6 +686,14 @@ function $RootScopeProvider(){ namedListeners = scope.$$listeners[name] || empty; event.currentScope = scope; for (i=0, length=namedListeners.length; i<length; i++) { + + // if listeners were deregistered, defragment the array + if (!namedListeners[i]) { + namedListeners.splice(i, 1); + i--; + length--; + continue; + } try { namedListeners[i].apply(null, listenerArgs); if (stopPropagation) return event; @@ -735,19 +743,29 @@ function $RootScopeProvider(){ }, defaultPrevented: false }, - listenerArgs = concat([event], arguments, 1); + listenerArgs = concat([event], arguments, 1), + listeners, i, length; //down while you can, then up and next sibling or up and next sibling until back at root do { current = next; event.currentScope = current; - forEach(current.$$listeners[name], function(listener) { + listeners = current.$$listeners[name] || []; + for (i=0, length = listeners.length; i<length; i++) { + // if listeners were deregistered, defragment the array + if (!listeners[i]) { + listeners.splice(i, 1); + i--; + length--; + continue; + } + try { - listener.apply(null, listenerArgs); + listeners[i].apply(null, listenerArgs); } catch(e) { $exceptionHandler(e); } - }); + } // Insanity Warning: scope depth-first traversal // yes, this code is a bit crazy, but it works and we have tests to prove it! |
