Dynamic segment in ember data adapter

一笑奈何 提交于 2019-12-19 10:11:08

问题


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:


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



来源:https://stackoverflow.com/questions/20699095/dynamic-segment-in-ember-data-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!