简体   繁体   中英

Ember-data model according recieved JSON

I get JSON with structure like this:

{
  "description":"text",
  "images":[{"id":"1","url":"image url"},{"id":"2","url":"url"}],
  "seats":3,
  "taken_seats":[{"number":1,"id":"1"},{"number":3,"id":"2"}],
  "title":"vel ad eius",
  "id":"1",
  "options":[]
}

How to structurize DS.model to handle it?

String values (description, title) will use a DS.attr('string')

Number (seats) will use a DS.attr('number')

Arrays (images, taken_seats, options) will use a relation like DS.hasMany('App.Image') , you will have to set the mapping in the adapter to embedded see here for details.

You may feel like you do not need a relationship for arrays, like for example if you do not want / can list the possible keys of object in the options array. The solution would be to register a custom transform for your needs then you may encounter some unexcepted behavior, particularly with the isDirty flag of the object, see here for details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM