2
votes

I'm making an app that retrieves data of an API which is not in my control. I have the following scenario:

The path for retrieving the posts is /api/posts. So I configured my ApplicationAdapter as follows:

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api'
});

The url for retrieving comments is '/api/posts/1/comments'. You can see that the url is prefixed by the path for retrieving a single post followed by the default path /comments.

Ember data defaults to /api/comments. But I want to configure an adapter for my Comment-model so it makes the correct url: /api/posts/:post_id/comments with :post_id replaced with the id of the current post. How do I do that?

1
Do you want to do /api/posts/1/comments just when you do post.get('comments') but when using store.find('comments'), the url will be /api/comments?Marcio Junior

1 Answers

2
votes

Modify your post json to include the hasMany as links (this could be done clientside), when it builds up the url it will prepend the url of the post, giving you post/1/comments

App.Post = DS.Model.extend({
   comments: DS.hasMany('comment', {async:true})
});

{
  post:{
    id: 1,
    links: {
      comments: 'comments'
    }
  }
}

Here's a dinky example with colors and items

http://emberjs.jsbin.com/OxIDiVU/68/edit