jeudi 7 avril 2016

What is best way to solve html input send data always string, when I use Ember data's DS.attr('boolean')?

I find a problem is handlebars input set the data. No matter what type of data, the end will be a string. If I use boolean or date, it will have a problem.

import DS from 'ember-data';

export default DS.Model.extend({
  ...
  sex: DS.attr('boolean'),
  createdAt: DS.attr('date'),
  updatedAt: DS.attr('date')
});

I use select to change the sex. http://ift.tt/1PG5iIQ

<select onchange={{action (mut model.sex) value="model.sex"}}>
  {{#each sexGroup as |sexChoice|}}
    <option value={{sexChoice.value}} selected={{eq model.sex sexChoice.value}}>{{sexChoice.name}}</option>
  {{/each}}
</select>

And set the sexGroup in controller:

sexGroup: [
  Ember.Object.create({value: 1, name: "man"}),
  Ember.Object.create({value: 0, name: "woman"})
]

The problem is whatever my select choice man or woman, Ember data will always send the true to server. Because html select value always is string, and Ember Data attr('boolean') get the string will become true.

If I must solve this problem, I have two way:

No.1(Not use boolean, usr string);

No.2(Use parseInt to set value before save), look like:

this.get('model').set('sex', parseInt(this.get('sexValue')));

The date format also have problem: What is the best way to modify the date format when ember-data does serialization?

So I don't know the role of Ember data boolean and date attribute types. Especially in the use of html input and the value always is string.

So there is a good solution to this situation? Or am I just using a string? Thanks.




Aucun commentaire:

Enregistrer un commentaire