samedi 3 octobre 2015

How JSON should be like when dealing with ember-data

i would like to know how the properties of a JSON should be liked when dealing with the ember-data library.

I was searching about it and i found that should be like this:

I want get all my BOOKINGS, for example, so should be like this:

{
  "data": [
   {
    "id": "209039090239390",
    "type": "booking",
    "attributes": {
      "name": "Booking one"
    }
   },
   {
    "id": "209039090239390",
    "type": "booking",
    "attributes": {
      "name": "Booking two"
    }
   }
  ]
}

So, in this case, i have two bookings, and each one have the name attribute. Perfect until now? Let's continue.

Now, in my Ember Model, HOW should i define this?? (That's what i want know)

Like this:

App.BookingModel = DS.Model.extend({
 type: DS.attr('string'),
 attributes: {
   name: DS.attr('string')
 }
});

Or like this:

App.BookingModel = DS.Model.extend({
 type: DS.attr('string'),
 name: DS.attr('string')
});

I'm using the first option, but i can't access the attributes object, because will throw an error:

Error while processing route: bookings Cannot read property '_attributes' of undefined

I already try to define as the second option, but than the result will be undefined, cause in my JSON, the NAME is inside attribute. I could change it and take off the attribute, but i want follow the convention..

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire