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

Commit bf0e837

Browse files
committed
fix($watchGroup): call listener once when the watchExpressions array is empty
1 parent 0554c1a commit bf0e837

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

src/ng/rootScope.js

+11
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,17 @@ function $RootScopeProvider(){
392392
var changeReactionScheduled = false;
393393
var firstRun = true;
394394

395+
if (!watchExpressions.length) {
396+
// No expressions means we call the listener ASAP
397+
var shouldCall = true;
398+
self.$evalAsync(function () {
399+
if (shouldCall) listener(newValues, newValues, self);
400+
});
401+
return function deregisterWatchGroup() {
402+
shouldCall = false;
403+
}
404+
}
405+
395406
if (watchExpressions.length === 1) {
396407
// Special case size of one
397408
return this.$watch(watchExpressions[0], function watchGroupAction(value, oldValue, scope) {

test/ng/rootScopeSpec.js

+37-16
Original file line numberDiff line numberDiff line change
@@ -844,59 +844,80 @@ describe('Scope', function() {
844844
}));
845845

846846

847-
it('should work for a group with just a single expression', function() {
848-
scope.$watchGroup(['a'], function(values, oldValues, s) {
847+
it('should detect a change to any one expression in the group', function() {
848+
scope.$watchGroup(['a', 'b'], function(values, oldValues, s) {
849849
expect(s).toBe(scope);
850850
log(oldValues + ' >>> ' + values);
851851
});
852852

853853
scope.a = 'foo';
854+
scope.b = 'bar';
854855
scope.$digest();
855-
expect(log).toEqual('foo >>> foo');
856+
expect(log).toEqual('foo,bar >>> foo,bar');
856857

857858
log.reset();
858859
scope.$digest();
859860
expect(log).toEqual('');
860861

861-
scope.a = 'bar';
862+
scope.a = 'a';
862863
scope.$digest();
863-
expect(log).toEqual('foo >>> bar');
864+
expect(log).toEqual('foo,bar >>> a,bar');
865+
866+
log.reset();
867+
scope.a = 'A';
868+
scope.b = 'B';
869+
scope.$digest();
870+
expect(log).toEqual('a,bar >>> A,B');
864871
});
865872

866873

867-
it('should detect a change to any one expression in the group', function() {
868-
scope.$watchGroup(['a', 'b'], function(values, oldValues, s) {
874+
it('should work for a group with just a single expression', function() {
875+
scope.$watchGroup(['a'], function(values, oldValues, s) {
869876
expect(s).toBe(scope);
870877
log(oldValues + ' >>> ' + values);
871878
});
872879

873880
scope.a = 'foo';
874-
scope.b = 'bar';
875881
scope.$digest();
876-
expect(log).toEqual('foo,bar >>> foo,bar');
882+
expect(log).toEqual('foo >>> foo');
877883

878884
log.reset();
879885
scope.$digest();
880886
expect(log).toEqual('');
881887

882-
scope.a = 'a';
888+
scope.a = 'bar';
883889
scope.$digest();
884-
expect(log).toEqual('foo,bar >>> a,bar');
890+
expect(log).toEqual('foo >>> bar');
891+
});
892+
893+
894+
it('should call the listener once when the array of watchExpressions is empty', function() {
895+
scope.$watchGroup([], function(values, oldValues) {
896+
log(oldValues + ' >>> ' + values);
897+
});
898+
899+
expect(log).toEqual('');
900+
scope.$digest();
901+
expect(log).toEqual(' >>> ');
885902

886903
log.reset();
887-
scope.a = 'A';
888-
scope.b = 'B';
889904
scope.$digest();
890-
expect(log).toEqual('a,bar >>> A,B');
905+
expect(log).toEqual('');
891906
});
892907

893908

894909
it('should not call watch action fn when watchGroup was deregistered', function() {
895-
var deregister = scope.$watchGroup(['a', 'b'], function(values, oldValues) {
910+
var deregisterMany = scope.$watchGroup(['a', 'b'], function(values, oldValues) {
911+
log(oldValues + ' >>> ' + values);
912+
}), deregisterOne = scope.$watchGroup(['a'], function(values, oldValues) {
913+
log(oldValues + ' >>> ' + values);
914+
}), deregisterNone = scope.$watchGroup([], function(values, oldValues) {
896915
log(oldValues + ' >>> ' + values);
897916
});
898917

899-
deregister();
918+
deregisterMany();
919+
deregisterOne();
920+
deregisterNone();
900921
scope.a = 'xxx';
901922
scope.b = 'yyy';
902923
scope.$digest();

0 commit comments

Comments
 (0)