mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-28 21:05:43 +08:00
feat(scope): only evaluate constant $watch expressions once
This commit is contained in:
@@ -300,6 +300,14 @@ function $RootScopeProvider(){
|
|||||||
watcher.fn = function(newVal, oldVal, scope) {listenFn(scope);};
|
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) {
|
if (!array) {
|
||||||
array = scope.$$watchers = [];
|
array = scope.$$watchers = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,14 @@ describe('Scope', function() {
|
|||||||
expect(spy).wasCalled();
|
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() {
|
it('should delegate exceptions', function() {
|
||||||
module(function($exceptionHandlerProvider) {
|
module(function($exceptionHandlerProvider) {
|
||||||
@@ -119,10 +127,14 @@ describe('Scope', function() {
|
|||||||
var log = '';
|
var log = '';
|
||||||
$rootScope.$watch('a', function() { log += 'a'; });
|
$rootScope.$watch('a', function() { log += 'a'; });
|
||||||
$rootScope.$watch('b', function() { log += 'b'; });
|
$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('c', function() { log += 'c'; });
|
||||||
|
$rootScope.$watch('2', function() { log += '2'; });
|
||||||
$rootScope.a = $rootScope.b = $rootScope.c = 1;
|
$rootScope.a = $rootScope.b = $rootScope.c = 1;
|
||||||
$rootScope.$digest();
|
$rootScope.$digest();
|
||||||
expect(log).toEqual('abc');
|
expect(log).toEqual('ab1c2');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user