Textfields only save strings and cause unexpected dirtyness in ember-data models

I’m having an issue where, when clicking on a textfield bound to a DS.attr(‘number’) attribute and then clicking out without changing anything, ember-data does no type coercion and so is changing the type of the bound attribute to a string, and setting it’s model to dirty.

In my application, I’ve extended Ember.Textfield to immediately save() the model if it’s dirty in the focusOut action, so that the user doesn’t have to click any save button. This results in many unnecessary server calls and weird behaviour.

An example of the issue: http://emberjs.jsbin.com/EnOqUxe/49/edit

What seems to happen is:

  1. model is clean
  2. user tabs in and out of textfield
  3. focusOut action runs, model is still clean so no server call happens
  4. ember-data updates the field to be a string, and sets the model to dirty
  5. user tabs in and out of textfield
  6. focusOut action runs, model is dirty and a server call happens
  7. promise returns and ember-data updates the field to be number, and sets the model to clean
  8. and so on repeatedly…

Other UI elements in my application can also depend on the model being dirty or not, so this changing causes other quirks. As a solution, I’ve created a custom Textfield that coerces it’s value into a number on focusOut.

Some questions if anyone has further insight:

  1. Is my ForcedNumberField component a decent/best fix?
  2. Is this a bug, or is it the intended behavior for ember’s TextField component - I’d at least expect my example of NumberField to cause no changes like the ForcedNumberField does?
  3. Is this the intended behaviour for ember-data?
  4. Why is type coercion in ember-data only being done
    for serialization/deserialization and not for the models? I see
    https://github.com/emberjs/data/issues/608 but it seems to have been closed without much discussion/explanation.
1 Like

Also see issue https://github.com/emberjs/data/issues/1540 which is still open.

1 Like