fix($watchGroup): call listener once when the watchExpressions array is empty

This commit is contained in:
rodyhaddad
2014-08-14 11:33:58 -07:00
parent 0554c1aae4
commit bf0e83732a
2 changed files with 55 additions and 23 deletions

View File

@@ -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) {

View File

@@ -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();