Willem de Wit
Willem de Wit

Reputation: 8732

Dynamic segment in ember data adapter

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?

Upvotes: 2

Views: 764

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

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

Upvotes: 2

Related Questions