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

Commit

Permalink
fix(Scope): ensure that a scope is destroyed only once
Browse files Browse the repository at this point in the history
Due to bd524fc calling $destroy() on a scope mupltiple times cases NPE.

Closes #1627
  • Loading branch information
IgorMinar committed Nov 30, 2012
1 parent 5f7054b commit d6da505
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function $RootScopeProvider(){
this.$$nextSibling = this.$$prevSibling =
this.$$childHead = this.$$childTail = null;
this['this'] = this.$root = this;
this.$$destroyed = false;
this.$$asyncQueue = [];
this.$$listeners = {};
}
Expand Down Expand Up @@ -467,10 +468,12 @@ function $RootScopeProvider(){
* perform any necessary cleanup.
*/
$destroy: function() {
if ($rootScope == this) return; // we can't remove the root node;
// we can't destroy the root scope or a scope that has been already destroyed
if ($rootScope == this || this.$$destroyed) return;
var parent = this.$parent;

this.$broadcast('$destroy');
this.$$destroyed = true;

if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
Expand Down
16 changes: 16 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,22 @@ describe('Scope', function() {
first.$destroy();
expect(log).toEqual('first; first-child');
}));


it('should $destroy a scope only once and ignore any further destroy calls',
inject(function($rootScope) {
$rootScope.$digest();
expect(log).toBe('123');

first.$destroy();
first.$apply();
expect(log).toBe('12323');

first.$destroy();
first.$destroy();
first.$apply();
expect(log).toBe('1232323');
}));
});


Expand Down

0 comments on commit d6da505

Please sign in to comment.