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

Commit 1c9fc1e

Browse files
committed
fix(scope): rerun $digest from root, rather then per scope.
1 parent 8bc7bea commit 1c9fc1e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Scope.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,15 @@ Scope.prototype = {
389389
watch, value, last,
390390
watchers = this.$$watchers,
391391
length, count = 0,
392-
iterationCount, ttl = 100;
392+
dirtyCount, ttl = 100,
393+
recheck = !this.$parent || !this.$parent.$$phase;
393394

394395
if (this.$$phase) {
395396
throw Error(this.$$phase + ' already in progress');
396397
}
397398
this.$$phase = '$digest';
398399
do {
399-
iterationCount = 0;
400+
dirtyCount = 0;
400401
if (watchers) {
401402
// process our watches
402403
length = watchers.length;
@@ -406,7 +407,7 @@ Scope.prototype = {
406407
// Most common watches are on primitives, in which case we can short
407408
// circuit it with === operator, only when === fails do we use .equals
408409
if ((value = watch.get(this)) !== (last = watch.last) && !equals(value, last)) {
409-
iterationCount++;
410+
dirtyCount++;
410411
watch.fn(this, watch.last = copy(value), last);
411412
}
412413
} catch (e) {
@@ -416,14 +417,14 @@ Scope.prototype = {
416417
}
417418
child = this.$$childHead;
418419
while(child) {
419-
iterationCount += child.$digest();
420+
dirtyCount += child.$digest();
420421
child = child.$$nextSibling;
421422
}
422-
count += iterationCount;
423+
count += dirtyCount;
423424
if(!(ttl--)) {
424425
throw Error('100 $digest() iterations reached. Aborting!');
425426
}
426-
} while (iterationCount);
427+
} while (recheck && dirtyCount);
427428
this.$$phase = null;
428429
return count;
429430
},

test/ScopeSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ describe('Scope', function(){
178178
expect(log).toEqual('abc');
179179
});
180180

181+
it('should repeat watch cycle from the root elemnt', function(){
182+
var log = '';
183+
var child = root.$new();
184+
root.$watch(function(){ log += 'a'; });
185+
child.$watch(function(){ log += 'b'; });
186+
root.$digest();
187+
expect(log).toEqual('abab');
188+
});
189+
181190

182191
it('should prevent infinite recursion', function(){
183192
root.$watch('a', function(self, v){self.b++;});

0 commit comments

Comments
 (0)