-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat(ngModel) Allow running the formatters without a change to the modelValue #10764
Conversation
It's a simple change and I think we should merge this. Let's see what @petebacondarwin says. |
Oh, since ngModel is planned to change quite a lot in 1.4, it probably only makes sense if we merge this into 1.3.x, too |
I would prefer the new API to be of the form: ngModel.$setModelValue(ngModel.$modelValue); Would this cause any problems if it updates the |
@Narretz I've removed the validation from @petebacondarwin If it would be done this way, shouldn't only |
@petebacondarwin I like the idea to make it analogous to @realityking I don't think |
@Narretz I can changed it to |
ctrl.$setModelValue(5); | ||
expect(ctrl.$render).not.toHaveBeenCalled(); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other tests to consider:
- that the formatters are run
- that the validators are run
- what happens if something goes wrong (say a formatter fails) to the modelValue/viewValue/validity
- do the formatters/validators run if the modelValue has not actually changed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the API is ok, most tests from the next section (model -> view) could be moved here. model -> view basically only needs to tests that it calls $setModelValue if the model value has changed.
Also what is the relationship between the model expression on the scope and the |
@petebacondarwin that's a good question, I'll test it to make sure |
ae8f562
to
6cc36f2
Compare
@petebacondarwin I finally got back to this. Indeed the value is override on the next digest. (I've added a test for this) I've managed to work around it, but I'm not sure if the |
I ran into this thread while researching a similar problem. My question about adding something to the API would be "why doesn't ngModel watch for changes in formatters and do this on it's own (without adding an API call)?". |
@joeenzminger While nice from DX perspective (however such an API would still be necessary if something internal to the formatter changes), I don't think it's worth adding two watchers for each ngModel. |
I guess it revolves around how expensive running the formatters is. I don't think it would require adding an extra watch, but it would make the existing watch more expensive (you'd have to run the formatters each digest to see if something internal or external changed). But I see your point. |
@petebacondarwin Any news on this? |
I will take a look this week. |
@petebacondarwin ping |
expect(ctrl.$modelValue).toBe(10); | ||
}); | ||
|
||
it('should update the value on the scope', inject(function($compile) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually looking at this again I don't like that we can set the $modelValue
to a value that is different to the scope.
For your use case @realityking you should always call this method as ngModel.$setModelValue(ngModel.$modelValue)
and providing a different value should be documented as not supported.
This would remove the need for this test and the associated behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the follow up.
This sounds like a bad API (you can call this function only with this value, nothing else). To be honest, given this comment, I'd be more comfortable returning to $runFormatters
(maybe called $format
to be more inline with the other methods in ngModelController
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I guess you are right.
If we had ngModel.$formatModel
, then I suspect we would need to have a complementary ngModel.$parseView
? Since changes to the formatters might well include changes to the parsers too? In which case one would want the new viewValue to be parsed and update the model....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a decent idea, even if only for API symmetry reasons. (I believe you can already gain the effect by calling '$setViewValue` with the current view value)
I'll try to get it done over the weekend.
I am going to be away from next week: @Narretz could you take over this one? |
Just a small FYI, I've been working on this a bit but haven't got it done yet. Maybe I get to it this weekend. |
Any progress with this? |
Is this going to be merged ? |
It would be great to have this functionality in there – @petebacondarwin / @realityking, do you have any plans to finish the last bit of this PR so it can be merged? |
This is needed when a formatter can be configured in some way and needs to be rerun after the change. Issue propped up for me when I tried to fix UI Bootstrap with Angular 1.3 (angular-ui/bootstrap#2659)
Fixes #3407