From 86ab02fa33dfee5af4e4f5f80377dd319d23e05f Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 9 Sep 2014 12:01:15 -0700 Subject: [PATCH] fix(ngModel): render immediately also with async validators --- src/ng/directive/input.js | 11 +++++------ test/ng/directive/inputSpec.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index e3240c744538..2a70aad77d85 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -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); } } diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index e4a3e303afe2..9104af021e00 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -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() {