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

Commit

Permalink
feat(scope): only evaluate constant $watch expressions once
Browse files Browse the repository at this point in the history
  • Loading branch information
mernen authored and mhevery committed Feb 14, 2013
1 parent 1ed6385 commit 1d7a95d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ function $RootScopeProvider(){
watcher.fn = function(newVal, oldVal, scope) {listenFn(scope);};
}

if (typeof watchExp == 'string' && get.constant) {
var originalFn = watcher.fn;
watcher.fn = function(newVal, oldVal, scope) {
originalFn.call(this, newVal, oldVal, scope);
arrayRemove(array, watcher);
};
}

if (!array) {
array = scope.$$watchers = [];
}
Expand Down
14 changes: 13 additions & 1 deletion test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ describe('Scope', function() {
expect(spy).wasCalled();
}));

it('should not keep constant expressions on watch queue', inject(function($rootScope) {
$rootScope.$watch('1 + 1', function() {});
expect($rootScope.$$watchers.length).toEqual(1);
$rootScope.$digest();

expect($rootScope.$$watchers.length).toEqual(0);
}));


it('should delegate exceptions', function() {
module(function($exceptionHandlerProvider) {
Expand All @@ -119,10 +127,14 @@ describe('Scope', function() {
var log = '';
$rootScope.$watch('a', function() { log += 'a'; });
$rootScope.$watch('b', function() { log += 'b'; });
// constant expressions have slightly different handling,
// let's ensure they are kept in the same list as others
$rootScope.$watch('1', function() { log += '1'; });
$rootScope.$watch('c', function() { log += 'c'; });
$rootScope.$watch('2', function() { log += '2'; });
$rootScope.a = $rootScope.b = $rootScope.c = 1;
$rootScope.$digest();
expect(log).toEqual('abc');
expect(log).toEqual('ab1c2');
}));


Expand Down

0 comments on commit 1d7a95d

Please sign in to comment.