mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
fix($rootScope.$on) check listener existense while deregistering
Check that listener is still present in $$listeners before decrease $$listenerCount. It fixes problem with incorrect $$listenerCount after call deregistering function multiple times. Closes #9666 Closes #9667
This commit is contained in:
committed by
Peter Bacon Darwin
parent
ed3f799b5c
commit
bf9e7bfb5a
@@ -1103,8 +1103,11 @@ function $RootScopeProvider(){
|
||||
|
||||
var self = this;
|
||||
return function() {
|
||||
namedListeners[namedListeners.indexOf(listener)] = null;
|
||||
decrementListenerCount(self, 1, name);
|
||||
var indexOfListener = namedListeners.indexOf(listener);
|
||||
if (indexOfListener !== -1) {
|
||||
namedListeners[indexOfListener] = null;
|
||||
decrementListenerCount(self, 1, name);
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
@@ -1638,6 +1638,31 @@ describe('Scope', function() {
|
||||
expect(child1.$$listenerCount).toEqual({event1: 1});
|
||||
expect(child2.$$listenerCount).toEqual({});
|
||||
}));
|
||||
|
||||
|
||||
it('should not decrement $$listenerCount when called second time', inject(function($rootScope) {
|
||||
var child = $rootScope.$new(),
|
||||
listener1Spy = jasmine.createSpy(),
|
||||
listener2Spy = jasmine.createSpy();
|
||||
|
||||
child.$on('abc', listener1Spy);
|
||||
expect($rootScope.$$listenerCount).toEqual({abc: 1});
|
||||
expect(child.$$listenerCount).toEqual({abc: 1});
|
||||
|
||||
var deregisterEventListener = child.$on('abc', listener2Spy);
|
||||
expect($rootScope.$$listenerCount).toEqual({abc: 2});
|
||||
expect(child.$$listenerCount).toEqual({abc: 2});
|
||||
|
||||
deregisterEventListener();
|
||||
|
||||
expect($rootScope.$$listenerCount).toEqual({abc: 1});
|
||||
expect(child.$$listenerCount).toEqual({abc: 1});
|
||||
|
||||
deregisterEventListener();
|
||||
|
||||
expect($rootScope.$$listenerCount).toEqual({abc: 1});
|
||||
expect(child.$$listenerCount).toEqual({abc: 1});
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user