Skip to content

Commit

Permalink
fix(ngModel): render immediately also with async validators
Browse files Browse the repository at this point in the history
  • Loading branch information
tbosch authored and Michael Gallagher committed Sep 10, 2014
1 parent 4e9e7d3 commit 86ab02f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/ng/directive/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 86ab02f

Please sign in to comment.