Ember-data store my attr('number') as a string!? Why?


#1

Hello,

I define this model:

import DS from 'ember-data';

export default DS.Model.extend({
    nb: DS.attr('number')
});

And, in my template, i use:

{{input name="mynb" value=(mut model.nb)}}

But when i use nb in my controller after changed its value with this input, nb become a string?! I thought ember-data cast it to a number before store it (and not only when i send my modified model to my backend server). No?

Will we have to create an action in the controller or a Computed Property in the model to perform this type casting :disappointed_relieved:?

Thanks for your help,


#2

Um, because native input tag stores it string?

Also, that type you give to DS.attr only get looked at by serializer. It does the conversion when it get send to the server, but not before. If you have a custom serializer, it’s possible this step is skipped altogether.


#3

Because input will casting this value to strings, it always does even if you specify type=number, no need to worry it to much because ember data serializer will handle that for you, but if you need to do mathematic calculation in other layer, components for example, you need to cast it back to number explicitly.


#4

Thank you.

I’s actually what I understood :confused: . Ember-data does not auto cast the attributes when store the data. So I created a component {{numeric-field}} to be sure to return a Number.

see Number attributes don’t convert values properly when used with input helpers #11209