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

Commit 874cac8

Browse files
teropalgalfaso
authored andcommitted
fix($parse): stabilize one-time literal expressions correctly
Change `oneTimeLiteralWatchDelegate` to use the last value in the digest cycle to check if the literal value is stable
1 parent 57f804a commit 874cac8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/ng/parse.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1168,16 +1168,17 @@ function $ParseProvider() {
11681168
}
11691169

11701170
function oneTimeLiteralWatchDelegate(scope, listener, objectEquality, parsedExpression) {
1171-
var unwatch;
1171+
var unwatch, lastValue;
11721172
return unwatch = scope.$watch(function oneTimeWatch(scope) {
11731173
return parsedExpression(scope);
11741174
}, function oneTimeListener(value, old, scope) {
1175+
lastValue = value;
11751176
if (isFunction(listener)) {
11761177
listener.call(this, value, old, scope);
11771178
}
11781179
if (isAllDefined(value)) {
11791180
scope.$$postDigest(function () {
1180-
if(isAllDefined(value)) unwatch();
1181+
if(isAllDefined(lastValue)) unwatch();
11811182
});
11821183
}
11831184
}, objectEquality);

test/ng/parseSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,28 @@ describe('parser', function() {
13011301
expect($rootScope.$$watchers.length).toBe(0);
13021302
expect(log.empty()).toEqual([]);
13031303
}));
1304+
1305+
it('should only become stable when all the elements of an array have defined values at the end of a $digest', inject(function($parse, $rootScope, log) {
1306+
var fn = $parse('::[foo]');
1307+
$rootScope.$watch(fn, function(value) { log(value); }, true);
1308+
$rootScope.$watch('foo', function() { if ($rootScope.foo === 'bar') {$rootScope.foo = undefined; } });
1309+
1310+
$rootScope.foo = 'bar';
1311+
$rootScope.$digest();
1312+
expect($rootScope.$$watchers.length).toBe(2);
1313+
expect(log.empty()).toEqual([['bar'], [undefined]]);
1314+
1315+
$rootScope.foo = 'baz';
1316+
$rootScope.$digest();
1317+
expect($rootScope.$$watchers.length).toBe(1);
1318+
expect(log.empty()).toEqual([['baz']]);
1319+
1320+
$rootScope.bar = 'qux';
1321+
$rootScope.$digest();
1322+
expect($rootScope.$$watchers.length).toBe(1);
1323+
expect(log).toEqual([]);
1324+
}));
1325+
13041326
});
13051327
});
13061328

0 commit comments

Comments
 (0)