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

Commit

Permalink
fix($rootScope): allow destroying a root scope
Browse files Browse the repository at this point in the history
When running (i.e. bootstrapping and killing) multiple apps on the same page,
it makes sense to destroy the root scope.

Closes #11241
Closes #10895
  • Loading branch information
vojtajina authored and petebacondarwin committed Mar 20, 2015
1 parent 9f7a80c commit f8c8cf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,13 +871,12 @@ function $RootScopeProvider() {
* clean up DOM bindings before an element is removed from the DOM.
*/
$destroy: function() {
// we can't destroy the root scope or a scope that has been already destroyed
// We can't destroy a scope that has been already destroyed.
if (this.$$destroyed) return;
var parent = this.$parent;

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

incrementWatchersCount(this, -this.$$watchersCount);
for (var eventName in this.$$listenerCount) {
Expand All @@ -886,8 +885,8 @@ function $RootScopeProvider() {

// sever all the references to parent scopes (after this cleanup, the current scope should
// not be retained by any of our references and should be eligible for garbage collection)
if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
if (parent && parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
if (parent && parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling;
if (this.$$nextSibling) this.$$nextSibling.$$prevSibling = this.$$prevSibling;

Expand Down
25 changes: 21 additions & 4 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,16 +1021,33 @@ describe('Scope', function() {


it('should broadcast $destroy on rootScope', inject(function($rootScope) {
var spy = spyOn(angular, 'noop');
$rootScope.$on('$destroy', angular.noop);
var spy = jasmine.createSpy('$destroy handler');
$rootScope.$on('$destroy', spy);
$rootScope.$destroy();
$rootScope.$digest();
expect(log).toEqual('123');
expect(spy).toHaveBeenCalled();
expect($rootScope.$$destroyed).toBe(true);
}));


it('should remove all listeners after $destroy of rootScope', inject(function($rootScope) {
var spy = jasmine.createSpy('$destroy handler');
$rootScope.$on('dummy', spy);
$rootScope.$destroy();
$rootScope.$broadcast('dummy');
expect(spy).not.toHaveBeenCalled();
}));


it('should remove all watchers after $destroy of rootScope', inject(function($rootScope) {
var spy = jasmine.createSpy('$watch spy');
var digest = $rootScope.$digest;
$rootScope.$watch(spy);
$rootScope.$destroy();
digest.call($rootScope);
expect(spy).not.toHaveBeenCalled();
}));


it('should remove first', inject(function($rootScope) {
first.$destroy();
$rootScope.$digest();
Expand Down

0 comments on commit f8c8cf6

Please sign in to comment.