fix(ngModel): render immediately also with async validators

This commit is contained in:
Tobias Bosch
2014-09-09 12:01:15 -07:00
parent 9ad7d745ab
commit f94d551529
2 changed files with 30 additions and 6 deletions

View File

@@ -2152,12 +2152,11 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
while(idx--) {
viewValue = formatters[idx](viewValue);
}
var lastViewValue = ctrl.$viewValue;
if (lastViewValue !== viewValue) {
ctrl.$$runValidators(undefined, modelValue, viewValue, function() {
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
ctrl.$render();
});
if (ctrl.$viewValue !== viewValue) {
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
ctrl.$render();
ctrl.$$runValidators(undefined, modelValue, viewValue, noop);
}
}

View File

@@ -407,6 +407,31 @@ describe('NgModelController', function() {
scope.$apply('value = 5');
expect(ctrl.$render).toHaveBeenCalledOnce();
});
it('should render immediately even if there are async validators', inject(function($q) {
spyOn(ctrl, '$render');
ctrl.$asyncValidators.someValidator = function() {
return $q.defer().promise;
};
scope.$apply('value = 5');
expect(ctrl.$viewValue).toBe(5);
expect(ctrl.$render).toHaveBeenCalledOnce();
}));
it('should not rerender nor validate in case view value is not changed', function() {
ctrl.$formatters.push(function(value) {
return 'nochange';
});
spyOn(ctrl, '$render');
ctrl.$validators.spyValidator = jasmine.createSpy('spyValidator');
scope.$apply('value = "first"');
scope.$apply('value = "second"');
expect(ctrl.$validators.spyValidator).toHaveBeenCalledOnce();
expect(ctrl.$render).toHaveBeenCalledOnce();
});
});
describe('validations pipeline', function() {