jmsalcido
jmsalcido

Reputation: 1077

Ember-Data: RESTAdapter: It is possible to sideload an object list inside a sideloaded object?

Hello I am working with emberjs and ember-data and Im really new to javascript.

I'm following this guide from ember website: http://emberjs.com/guides/models/the-rest-adapter/

I want to know if im able to sideload an object list inside a sideloaded object with ember-data rest adapter, I am receiving the following output from the backend:

  {
    "search_flight" : { "flight_ids" : [
      166,
      4792
    ] },
  "flights" : [ 
      {
    "id" : 166,
    "arrival_airport" : "CUL",
    "date" : "Aug 14, 2013 12:00:00 AM",
    "departure_airport" : "MEX",
    "flight_detail_ids" : [ 166 ],
    "flight_details" : [ { 
          "id" : 166,
          "airline" : "Aeromexico",
          "arrival_airport" : "CUL",
          "arrival_time" : "16:48:00.000",
          "departure_airport" : "MEX",
          "departure_time" : "15:43:00.000",
          "flight_number" : "166",
          "travel_time" : 125
        } ],
    "flight_type" : 1,
    "travel_time" : 125
  },
  {
     //other flight...
  }]
 }

this is at my ember-data config:

App.Flight = DS.Model.extend({
    date: DS.attr('string'),
    departureAirport: DS.attr('string'),
    arrivalAirport: DS.attr('string'),
    travelTime: DS.attr('number'),
    flightType: DS.attr('number'),
    flightDetail: DS.hasMany('App.FlightDetail')
});

DS.RESTAdapter.configure('App.FlightDetail', {
   sideloadsAs: 'flight_details'
});

I don't know if I am being clear.

gist url: https://gist.github.com/jmsalcido/f46730922864e2456a5b

Upvotes: 1

Views: 914

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

Yes this is possible but it's not called sideloading - ember supports this technique but refers to it as embedded. So replace of the DS.RESTAdapter.configure {(.... sideLoadAs... try:

DS.RESTAdapter.map('App.Flight', {
  flightDetails: { embedded: 'always' }
};

Also since this is a hasMany relationship I would suggest changing your model definition to use the plural flightDetails: DS.hasMany('App.FlightDetail') instead of singular.

See this SO post for another example of embedded hasMany relationships:

How to make embedded hasMany relationships work with ember data

Upvotes: 2

Related Questions