Skip to content

Commit

Permalink
fix(scope): handle event registration/deregistration on destroyed scope
Browse files Browse the repository at this point in the history
When trying to register an event on a child scope of an already
destroyed scope, make this a noop
When trying to deregister an event on a child scope of an already
destroyed scope, make this a noop

Closes angular#6897
  • Loading branch information
lgalfaso committed Mar 28, 2014
1 parent 908ab52 commit a670929
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ function $RootScopeProvider(){
* @returns {function()} Returns a deregistration function for this listener.
*/
$on: function(name, listener) {
if (this.$$destroyed) return noop;
var namedListeners = this.$$listeners[name];
if (!namedListeners) {
this.$$listeners[name] = namedListeners = [];
Expand All @@ -937,6 +938,7 @@ function $RootScopeProvider(){

var self = this;
return function() {
if (self.$$destroyed) return noop;
namedListeners[indexOf(namedListeners, listener)] = null;
decrementListenerCount(self, 1, name);
};
Expand Down
20 changes: 20 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,26 @@ describe('Scope', function() {
$rootScope.$broadcast(EVENT);
expect(spy.callCount).toBe(1);
}));

it('should not throw when trying to listen to an event on a child scope of an already destroyed scope', inject(function($rootScope) {
var parent = $rootScope.$new(),
child = parent.$new();

parent.$destroy();
expect(function() {
var fn = child.$on('someEvent', angular.noop);
fn();
}).not.toThrow();
}));

it('should not throw when deregistering a listener on a child scope of an already destroyed scope', inject(function($rootScope) {
var parent = $rootScope.$new(),
child = parent.$new(),
fn = child.$on('someEvent', angular.noop);

parent.$destroy();
expect(function() { fn(); }).not.toThrow();
}));
});


Expand Down

0 comments on commit a670929

Please sign in to comment.