Skip to content

Commit

Permalink
feat(ngModel): expose function to run model -> view pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Narretz committed Sep 20, 2017
1 parent 5346772 commit ba8204a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/ng/directive/ngModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,27 @@ NgModelController.prototype = {
*/
$overrideModelOptions: function(options) {
this.$options = this.$options.createChild(options);
},

$setModelValue: function(modelValue) {
this.$modelValue = this.$$rawModelValue = modelValue;
this.$$parserValid = undefined;

var formatters = this.$formatters,
idx = formatters.length;

var viewValue = modelValue;
while (idx--) {
viewValue = formatters[idx](viewValue);
}
if (this.$viewValue !== viewValue) {
this.$$updateEmptyClasses(viewValue);
this.$viewValue = this.$$lastCommittedViewValue = viewValue;
this.$render();

// It is possible that model and view value have been updated during render
this.$$runValidators(this.$modelValue, this.$viewValue, noop);
}
}
};

Expand All @@ -903,24 +924,7 @@ function setupModelWatcher(ctrl) {
// eslint-disable-next-line no-self-compare
(ctrl.$modelValue === ctrl.$modelValue || modelValue === modelValue)
) {
ctrl.$modelValue = ctrl.$$rawModelValue = modelValue;
ctrl.$$parserValid = undefined;

var formatters = ctrl.$formatters,
idx = formatters.length;

var viewValue = modelValue;
while (idx--) {
viewValue = formatters[idx](viewValue);
}
if (ctrl.$viewValue !== viewValue) {
ctrl.$$updateEmptyClasses(viewValue);
ctrl.$viewValue = ctrl.$$lastCommittedViewValue = viewValue;
ctrl.$render();

// It is possible that model and view value have been updated during render
ctrl.$$runValidators(ctrl.$modelValue, ctrl.$viewValue, noop);
}
ctrl.$setModelValue(modelValue);
}

return modelValue;
Expand Down
53 changes: 53 additions & 0 deletions test/ng/directive/ngModelSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,59 @@ describe('ngModel', function() {
expect(ctrl.$modelValue).toBeNaN();

}));

describe('$setModelValue', function() {
it('should run the model -> view pipeline', function() {
var log = [];

ctrl.$formatters.unshift(function(value) {
log.push(value);
return value + 2;
});

ctrl.$formatters.unshift(function(value) {
log.push(value);
return value + '';
});

spyOn(ctrl, '$render');

ctrl.$setModelValue(3);

expect(ctrl.$modelValue).toBe(3);
expect(log).toEqual([3, 5]);
expect(ctrl.$viewValue).toBe('5');
expect(ctrl.$render).toHaveBeenCalledOnce();
});

it('should run the model -> view pipeline even if the value has not changed', function() {
// this is analogue to $setViewValue
spyOn(ctrl, '$render');

ctrl.$setModelValue(3);

expect(ctrl.$modelValue).toBe(3);
expect(ctrl.$render).toHaveBeenCalledOnce();

ctrl.$setModelValue(3);
expect(ctrl.$modelValue).toBe(3);
expect(ctrl.$render).toHaveBeenCalledOnce();
});

it('should not modify the scope value', function() {
// this is analogue to $setViewValue, which does not modify the rendered (DOM) value
scope.$apply('value = 10');
expect(ctrl.$modelValue).toBe(10);
expect(ctrl.$viewValue).toBe(10);

ctrl.$setModelValue(3);

expect(scope.value).toBe(10);
expect(ctrl.$modelValue).toBe(3);
expect(ctrl.$viewValue).toBe(3);
});

});
});


Expand Down

0 comments on commit ba8204a

Please sign in to comment.