Ember.Select using Ember-data model

I create a model with a numeric id. I have a list on the controller with numeric id’s.

When I use the list as content in an Ember.Select, and use the model.id as value, they don’t match (and due to the two-way binding, it overwrites the id field). The field model.id is in fact a String object now.

This is visualized in http://jsbin.com/hirapuhalipi/1/edit where I have a comment before a parseInt()-line. With this line in use, matching works, since I’ve told it to have the same type as the option-list in the select. Without the line, the above error is visible.

Why does it act like this? Shouldn’t id be a number if I create the record with it as a number? Should the Select have been used in another fashion?

It does this because it’s stored as a string in the identity map for the store.

https://github.com/emberjs/data/blob/39c91c062a5fec09c6c1ec5294f6ba6133638135/packages/ember-data/lib/system/store.js#L57 is the function that is getting called to coerce it to a string.

I suppose you could create a computed property on your model or controller idAsInt.

DS.Model.reopen({
  idAsInt: Ember.computed('id', function(propertyName, value) {
    if(arguments.length > 1) {
       this.set('id', value);  
    }
      
    return parseInt(this.get('id'), 10); 
  })  
});

Example on your controller: http://jsbin.com/hirapuhalipi/2/edit

Quick response here…:slight_smile:

I don’t really understand why it’s stored as a string like that, but computing a numeric copy that way seems to work at least, so it should be a way around the issue then.

Thanks for the suggestion.

I took @stefanberndtsson’s jsbin and uncommented the parseInt() logic to make his example work. But I noticed that while the select element property selects the correct one, if you change the select option, model.id changes but model.name doesn’t. Is there a way to use this Select View helper to change the model (id and name) based on the selected option? I tried using the ‘selection’ attribute instead of ‘value’ attribute, but that didn’t seem to work.