From bf0e83732aa02c7aa08d0ccdf122116235fcfa11 Mon Sep 17 00:00:00 2001 From: rodyhaddad Date: Thu, 14 Aug 2014 11:33:58 -0700 Subject: [PATCH] fix($watchGroup): call listener once when the watchExpressions array is empty --- src/ng/rootScope.js | 11 +++++++ test/ng/rootScopeSpec.js | 67 ++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 24b80a36..e6986f84 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -392,6 +392,17 @@ function $RootScopeProvider(){ var changeReactionScheduled = false; var firstRun = true; + if (!watchExpressions.length) { + // No expressions means we call the listener ASAP + var shouldCall = true; + self.$evalAsync(function () { + if (shouldCall) listener(newValues, newValues, self); + }); + return function deregisterWatchGroup() { + shouldCall = false; + } + } + if (watchExpressions.length === 1) { // Special case size of one return this.$watch(watchExpressions[0], function watchGroupAction(value, oldValue, scope) { diff --git a/test/ng/rootScopeSpec.js b/test/ng/rootScopeSpec.js index f2f699a8..40fe7f9c 100644 --- a/test/ng/rootScopeSpec.js +++ b/test/ng/rootScopeSpec.js @@ -844,26 +844,6 @@ describe('Scope', function() { })); - it('should work for a group with just a single expression', function() { - scope.$watchGroup(['a'], function(values, oldValues, s) { - expect(s).toBe(scope); - log(oldValues + ' >>> ' + values); - }); - - scope.a = 'foo'; - scope.$digest(); - expect(log).toEqual('foo >>> foo'); - - log.reset(); - scope.$digest(); - expect(log).toEqual(''); - - scope.a = 'bar'; - scope.$digest(); - expect(log).toEqual('foo >>> bar'); - }); - - it('should detect a change to any one expression in the group', function() { scope.$watchGroup(['a', 'b'], function(values, oldValues, s) { expect(s).toBe(scope); @@ -891,12 +871,53 @@ describe('Scope', function() { }); - it('should not call watch action fn when watchGroup was deregistered', function() { - var deregister = scope.$watchGroup(['a', 'b'], function(values, oldValues) { + it('should work for a group with just a single expression', function() { + scope.$watchGroup(['a'], function(values, oldValues, s) { + expect(s).toBe(scope); log(oldValues + ' >>> ' + values); }); - deregister(); + scope.a = 'foo'; + scope.$digest(); + expect(log).toEqual('foo >>> foo'); + + log.reset(); + scope.$digest(); + expect(log).toEqual(''); + + scope.a = 'bar'; + scope.$digest(); + expect(log).toEqual('foo >>> bar'); + }); + + + it('should call the listener once when the array of watchExpressions is empty', function() { + scope.$watchGroup([], function(values, oldValues) { + log(oldValues + ' >>> ' + values); + }); + + expect(log).toEqual(''); + scope.$digest(); + expect(log).toEqual(' >>> '); + + log.reset(); + scope.$digest(); + expect(log).toEqual(''); + }); + + + it('should not call watch action fn when watchGroup was deregistered', function() { + var deregisterMany = scope.$watchGroup(['a', 'b'], function(values, oldValues) { + log(oldValues + ' >>> ' + values); + }), deregisterOne = scope.$watchGroup(['a'], function(values, oldValues) { + log(oldValues + ' >>> ' + values); + }), deregisterNone = scope.$watchGroup([], function(values, oldValues) { + log(oldValues + ' >>> ' + values); + }); + + deregisterMany(); + deregisterOne(); + deregisterNone(); scope.a = 'xxx'; scope.b = 'yyy'; scope.$digest();