mercredi 9 novembre 2016

Retrieve ember data relationship with one API call

I've two models:

ticket:

export default DS.Model.extend({

  name: DS.attr('string'),
  email: DS.attr('string'),

  messages: DS.hasMany('message'),

})

messages:

export default DS.Model.extend({


  message: DS.attr('string'),

  date: DS.attr('date'),

  ticket: DS.belongsTo('ticket', { async: true }),

});

And a router with this model method:

  model(params) {
   return this.get('store').findRecord('ticket', params.id,  {reload: true, include: 'messages'});

 },

At the page reload all ok, only a call to tickets/:id is made but sometimes ember make a bunch of calls to messages/:id. Why don't use the included data and try to retrieve from the server?

I've tried async: false on hasMany relation but I've this error:

Error: Assertion Failed: You looked up the 'messages' relationship on a 'ticket' with id 15 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (`DS.hasMany({ async: true })`)

Any idea?

This is the GET tickets/:id response:

{"data":
{
"id":"15",
"type":"tickets",
"attributes":{...},

"relationships":{"messages":{"data":[{"id":"1478482584658","type":"messages"},{"id":"1478482588516","type":"messages"},{"id":"1478517720","type":"messages"},{"id":"1478517813","type":"messages"},{"id":"1478517893","type":"messages"},{"id":"1478530030","type":"messages"},{"id":"1478530032","type":"messages"},{"id":"1478533446","type":"messages"}]}}},

"included":[

{"id":"1478482584658","type":"messages","attributes":{...}},{"id":"1478482588516","type":"messages","attributes":{...}},
...
}
}
]
}

Why don't take data from included and make other calls to the server?




Aucun commentaire:

Enregistrer un commentaire