mercredi 26 août 2015

How to distinguish self-referential models in Ember

I have a friend model that has a self-reference to relatedFriends:

export default DS.Model.extend({
  friends: hasMany('friend', {inverse : 'relatedFriend'}),
  relatedFriend: belongsTo('friend', {inverse : 'friends'})
});

I'm running into an issue when loading a set of friends, and side-loading their related friends.

Say my route's model hook looks like this:

model() {
  return this.store.find(‘/friend’, {page: 1});
}

The server responds with 10 friend records for page 1. But, those friends have friends, and I want to sideload 3 related friends for each primary friend. So, I’ll also have 30 friends sideloaded.

Typically, for this type of response, my json would look something like

{
  friends: [
    {id: 1, ...
    {id: 2, ...
    {id: 2, ...
  ]
}

The problem is, if my response puts all these under a single friends key in my json, model won't be the single page of friends I requested; it will be all the friends (40 in this example).

How can I differentiate between the "primary" friends from my query, and the side-loaded related models?




Aucun commentaire:

Enregistrer un commentaire