xamenrax
xamenrax

Reputation: 1744

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?

Upvotes: 0

Views: 217

Answers (1)

Adrien Coquio
Adrien Coquio

Reputation: 4930

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

Upvotes: 1

Related Questions