Search code examples
ember.jsember-data

How can I model an object that has a plain array of strings with ember data?


How can I model an object that has a plain array of strings? If I do the following it shows me the emails but when modifying their value they are not updated.

The server sends/receives the following data:

"data":
[
    {
        "attributes" : {
            "id":1,
            "name":"Jhon",
            "emails":["[email protected]","[email protected]"]},
            "relationships": (,..}
        }
    }

    ...

Model:

export default Model.extend({
  id: attr('string'),
  name: attr('string'),
  emails: attr()
})

Template:

{{#each model.emails as |email|}}
    <input type="text" value={{email}}>
{{/each}}

Solution

  • as email is a list in your above json, so it is better to make your object design with one to many relationship as below:- here 2 objects are there 1]employee 2]emailList

    app/models/employee.js
    
    
    import DS from 'ember-data';
    export default Model.extend({
     id: DS.attr('string'),
     name: DS.attr('string'),
     emails: DS.hasMany('email-list'),
    })
    
    app/models/email-list.js
    
    import DS from 'ember-data';
    export default Model.extend({
     email: DS.attr('string'),
     employee: DS.belongsTo('employee')
    })