From 47f6d46e604185ddfdcc89b08e3fcdbda25969b1 Mon Sep 17 00:00:00 2001 From: Lucas Galfaso Date: Sun, 14 Sep 2014 18:47:37 +0200 Subject: [PATCH] refactor($scope): prevent multiple calls to listener on `$destroy` Prevent isolated scopes from having listeners that get called multiple times when on `$destroy` --- src/ng/rootScope.js | 1 + test/ng/rootScopeSpec.js | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 77cba19968ab..3d1d67f75675 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -196,6 +196,7 @@ function $RootScopeProvider(){ // ensure that there is just one async queue per $rootScope and its children child.$$asyncQueue = this.$$asyncQueue; child.$$postDigestQueue = this.$$postDigestQueue; + child.$on('$destroy', function() { child.$$destroyed = true; }); } else { // Only create a child scope class if somebody asks for one, // but cache it to allow the VM to optimize lookups. diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index e3168ec10e86..c21500b31902 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -994,6 +994,13 @@ describe('Scope', function() { expect(log).toBe('123'); })); + it('should broadcast the $destroy only once', inject(function($rootScope, log) { + var isolateScope = first.$new(true); + isolateScope.$on('$destroy', log.fn('event')); + first.$destroy(); + isolateScope.$destroy(); + expect(log).toEqual('event'); + })); it('should decrement ancestor $$listenerCount entries', inject(function($rootScope) { var EVENT = 'fooEvent',