Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngModelController): allow $overrideModelOptions to set updateOn #16352

Merged
merged 1 commit into from
Dec 5, 2017

Conversation

Narretz
Copy link
Contributor

@Narretz Narretz commented Nov 29, 2017

Also adds more docs about "default" events and how to override
ModelOptions.

Closes #16351

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

What is the current behavior? (You can also link to an open issue here)

What is the new behavior (if this is a feature change)?

Does this PR introduce a breaking change?

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:

@petebacondarwin
Copy link
Contributor

I think, due to inheritance, that implementing this is more complicated than it seems. If a child ngModel is inheriting the current ngModelOptions, then changing the updateOn field on the parent would need to tell all descendant ngModels to reapply their event handlers.

@Narretz
Copy link
Contributor Author

Narretz commented Nov 29, 2017

I've added a note that overrideModelOptions does not propagate the changes. This is a general problem - even the simple options like debounce, allowInvalid are not updated.
I personally think that this is an acceptable limitation, as overriding options should be a rather rare and specific operation.

* <div class="alert alert-warning">
* **Note:** overriding the model options with this function only affects the
* current `ngModelController` and does not propagate the new options to
* `ngModelOptions` directives that have initially inherited from it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, since createChild() returns a new instance, nobody has inherited from it. It might make sense to highlight that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the whole notice is irrelevant, as you can only $overrideModelOptions on ngModel, and nested ngModels are definitely "here be dragons" territory. Or should we mention that this doesn't change the options on ngModelOptions that this instance has inherited from?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think you are right. The inheritance is between ngModelOptions (which can be nested). ngModel just takes the options of its nearest ngModuleOptions and it can overwrite them. Where the corresponding ngModelOptions got its values (inherited or not) is not affected (nor is there any reason to assume it would).

@@ -1321,9 +1348,7 @@ var ngModelDirective = ['$rootScope', function($rootScope) {
post: function ngModelPostLink(scope, element, attr, ctrls) {
var modelCtrl = ctrls[0];
if (modelCtrl.$options.getOption('updateOn')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check could be skipped here.

}

this.$$updateEvents = this.$options.getOption('updateOn');
this.$$element.on(this.$$updateEvents, this.$$updateEventHandler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to avoid this call if $$updateEvents are empty. (Several operations take place unnecessarily.)

* matches the default events belonging to the control. These are the events that are bound to
* the control, and when fired, update the `$viewValue` via `$setViewValue`.
*
* `ngModelOptions` considers every event that is not listed in `updateOn` a "default" event,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😞

* ### Default events, extra triggers, and catch-all debounce values
*
* This example shows the relationship between "default" update events and
* additional `updateOn`triggers.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing whitespace before triggers.

});
* </file>
* <file name="template.html">
<form name="$ctrl.form">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too little indentation.

<form name="$ctrl.form">
Input: <input type="text" name="input" ng-model="$ctrl.name" ng-model-options="$ctrl.options" />
</form>
Model: <tt>{{$ctrl.name}}</tt>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too much indentation.

<input type="submit" value="Update options">
</form>
* </file>
* </example>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one advanced example indeed. I am not even sure what it does 😛

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you read the description? 😛

@Narretz Narretz force-pushed the fix-ngmodeloptions-updateon branch from 3984ce4 to baac542 Compare December 4, 2017 11:55
Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - despite @gkalpak's sad face.
The extra explanatory docs are great.

};

this.updateEvents = function() {
var eventL = this.options.updateOn.split(' ');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventL?

Also adds more docs about "default" events and how to override
ModelOptions.

Closes angular#16351
@Narretz Narretz force-pushed the fix-ngmodeloptions-updateon branch from baac542 to 7f59fa1 Compare December 5, 2017 15:21
@Narretz Narretz merged commit da72477 into angular:master Dec 5, 2017
Narretz added a commit to Narretz/angular.js that referenced this pull request Dec 7, 2017
Also adds more docs about "default" events and how to override
ngModelController options.

Closes angular#16351
Closes angular#16352
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better docs for ngModelOptions updateOn default events etc
4 participants