Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember Data: Using "links" on JSON payload for hasMany relationships

Given two models in an app using DS.RESTAdapter:

App.Calendar = DS.Model.extend({
  reservations: DS.hasMany("reservation", { async: true })
});

App.Reservation = DS.Model.extend({
  date: DS.attr("date"),
  calendar: DS.belongsTo("calendar")
});

And payloads such as:

/api/calendar/1:

{
  "calendar": {
     "id": 1,
     "reservations": [],
     "links": {
        "reservations": "/api/calendar/1/reservations"
     }
  }
}

/api/calendar/1/reservations:

{
  "reservations": [
     {
        "id": 1,
        "date": "10/01/2014"
     }
  ]
}

Why is it that the reservations array on the Calendar model isn't being lazy-loaded?

like image 877
Joel A. Villarreal Bertoldi Avatar asked Oct 21 '22 02:10

Joel A. Villarreal Bertoldi


1 Answers

Your json shouldn't have reservations defined twice

{
  "calendar": {
     "id": 1,
     "links": {
        "reservations": "/api/calendar/1/reservations"
     }
  }
}
like image 94
Kingpin2k Avatar answered Oct 23 '22 01:10

Kingpin2k