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

feat(ngModelOptions): Model update behavior can now be customized - #6945

Closed
lrlopez wants to merge 4 commits into
angular:masterfrom
lrlopez:ng-model-options-rebase
Closed

lrlopez wants to merge 4 commits into
angular:masterfrom
lrlopez:ng-model-options-rebase

Conversation

@lrlopez

@lrlopez lrlopez commented Apr 1, 2014

Copy link
Copy Markdown
Contributor

By default, any change on the content will trigger an immediate model update and form validation. This PR implements a new directive ng-model-options that allow you to override this default behavior in several ways.

You should specify an object with the different parameters.

For example, it allows to trigger an update only when a particular event or list of events is received by the input using the updateOn key. Should you need multiple events, just use a space delimited string.

I.e. ng-model-options="{ updateOn: 'blur' }" will update and validate only after the control loses focus.

If you want to keep the default behavior and just add new events that may trigger the model update and validation, add "default" as one of the specified events.

I.e. ng-model-options="{ updateOn: 'default submit' }"

Also, with the debounce option, ng-model-options will allow deferring the actual model update until a timer expires. The timer will be reset each time an event is triggered.

I.e. `ng-model-options="{ debounce: 500 }" for 500ms after the latest event.

Custom timeouts for each event can be set for each event if you use an object in debounce. This can be useful to force immediate updates on some specific circumstances (like blur events).

I.e. ng-model-options="{ updateOn: 'default blur', debounce: { default: 500, blur: 0} }"

You can use the directive in any tag so its contents became the default settings for any child control, although they can be overridden.

Closes #1285, #2129

@mary-poppins

Copy link
Copy Markdown

Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.

  • Uses the issue template (#6945)

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

@Narretz

Narretz commented Apr 2, 2014

Copy link
Copy Markdown
Contributor

What about setting $dirty etc. when there's a viewValue, but the model hasn't been set? Is this supported or is everything delayed?

Comment thread docs/content/guide/forms.ngdoc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we have a simpler title here? Perhaps even split out the two different features ("custom triggers" and "debounced updates") into two sections?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure.

@lrlopez

lrlopez commented Apr 2, 2014

Copy link
Copy Markdown
Contributor Author

@Narretz, everything is delayed until the model is set.

Comment thread docs/content/guide/forms.ngdoc Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should probably make it clear that no update of any kind occurs till the debounce resolved. I.E. No parsers, nor validators are run and $pristine is not affected.

By default, any change on the content will trigger an immediate model update
and form validation. This PR implements a new directive `ng-model-options`
that allow you to override this default behavior in several ways.

You should specify an object with the different parameters.

For example, it allows to trigger an update only when a particular event or
list of events is received by the input using the `updateOn` key. Should you
need multiple events, just assign an array to it.

I.e. `ng-model-options="{ updateOn: 'blur' }"` will update and validate only
after the control loses focus.

If you want to keep the default behavior and just add new events that may
trigger the model update and validation, add "default" as one of the specified
events.

I.e. `ng-model-options="{ updateOn: ['default','submit'] }"`

Also, with the `debounce` option, `ng-model-options` will allow deferring the
actual model update until a timer expires. The timer will be reset each time
an event is triggered.

I.e. `ng-model-options="{ debounce: 500 }" for 500ms after the latest event.

Custom timeouts for each event can be set for each event if you use an object
in `debounce`. This can be useful to force immediate updates on some specific
circumstances (like blur events).

I.e. `ng-model-options="{ updateOn: ['default', 'blur'], debounce: { default: 500, blur: 0} }"`

You can use the directive in any tag so its contents became the default settings
for any child control, although they can be overridden.

Closes angular#1285, angular#2129
@lrlopez

lrlopez commented Apr 2, 2014

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions @caitp and @IgorMinar. They make the PR simpler. I've amended the commit.

Comment thread src/ng/directive/input.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we have to support list of events as an array? if we said that only space delimited strings are allowed ('blur click') then we wouldn't need forEach when registering listeners jquery/jqlite on support space delimited list of events and we could generally simplify quite a bit of code. (var events = options.$updateOn || options.$updateOnDefault; element.on(events, function() { ... });)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

or even

element.on(options.$updateOn || options.$updateOnDefault, function() { ... });

@IgorMinar

Copy link
Copy Markdown
Contributor

ok, I know that I left a lot of comments. @lrlopez, I don't mean to discourage you. This is a really good change, it just needs a bit more polish and then we can ship it.

Good work! (also @petebacondarwin and @auser)

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.

Please provide option for binding ng-model for input onblur

9 participants