mercredi 27 mai 2015

Ember-data - Sails - lazy fetch of hasMany relationship at a specific url, without payload links or ids

Using the classical models:

app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany('comment', {async: true})
});

app/models/comment.js
export default DS.Model.extend({
  post: DS.belongsTo('post', {async: true})
});

If I ask my sails API for

/posts/

will return this payload:

[
  {
    "id": 1
  },
  {
    "id": 3
  }
]
// those are posts

No sideloading of comments, not even a link. This is as intended. What my API has is a specific url to load a post's related comments (generated by the standard sails blueprints):

/posts/1/comment/

[
  {
    "post": 1,
    "id": 1,
  },
  {
    "post": 1,
    "id": 2
  },
  {
    "post": 1,
    "id": 3
  }
]
// those are comments

When I access a post's related comments through the hasMany relationship, typically in a template like this:

<ul>
  {{#each comment in post.comments}}
    <li>My comment id: {{comment.id}}!</li>
  {{/each}}
</ul>

I would like ember to load my currently-not-fetched related comments at the specified url. There are 2 not really documented methods on the RestAdapter that I though I could override for this: findHasMany and/or urlForFindHasMany, but those functions are never called by ember-data. I assume they need a link to be provided by the API. I have hacked my object controllers to fetch the data anyway, but I feel dirty doing this and I'd rather move the logic to the adapter.

Is there a way to achieve lazy loading of hasMany relations through a url of the following form?

/parentModel/:id/relatedModel/




Aucun commentaire:

Enregistrer un commentaire