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

Formatters and parsers consequences #494

Open
@redhead

Description

@redhead

I don't really like the current implementation of parsers and formatters, as for example these lines

https://github.com/formly-js/angular-formly/blob/master/src/directives/formly-field.js#L373-L378

can have unpredictable consequences (as it already had with making form dirty before the fix or rather "workaround").

I am facing a problem when using UI Bootstrap typeahead component. When I already have a model value set, and it is an object, I need to use a formatter so it displays some property of the model object when the field is rendered for the first time.

But if I use a formatter from formly, it triggers setViewValue which consequently triggers parser as well. For typeahead, setting a viewValue means the user has written something to the field resulting in calling the typeahead async options retrieval request to the server and displaying the results on screen.

The following happens when I just render a form on screen for the first time while having a model value already set:

  1. formatters are set and called to format a model
  2. setViewValue is called
  3. parsers are called
  4. typeahead's parser that queries the options is called
  5. typeahead options get visible on page

First question, I don't really know how to handle this situation. I need a formatter because the model value is an object which I cannot "render" in the text field, unless formatted to a string (e.g. using some object property). But this implementation triggers the options retrieval and making them visible.

Second question, do you think this could be somehow rethought, for example adding a wrapping ngModel directive which would call some function on field options to get formatters/parsers and set them in the ngModel link function instead of much much later where we are forced to use setViewValue and setPristine and other workarounds so the formatters work?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions