ember 数据适配器中的动态段 [英] Dynamic segment in ember data adapter
问题描述
我正在制作一个应用程序,用于检索不在我控制范围内的 API 数据.我有以下场景:
I'm making an app that retrieves data of an API which is not in my control. I have the following scenario:
检索帖子的路径是/api/posts
.所以我配置了我的 ApplicationAdapter 如下:
The path for retrieving the posts is /api/posts
. So I configured my ApplicationAdapter as follows:
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'api'
});
检索评论的网址是/api/posts/1/comments".您可以看到该 url 以检索单个 post
的路径为前缀,后跟默认路径 /comments
.
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 数据默认为 /api/comments
.但我想为我的 Comment
-model 配置一个适配器,以便它生成正确的 url:/api/posts/:post_id/comments
和 :post_id
替换为当前帖子的 id.我该怎么做?
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?
推荐答案
修改你的 post json 以包含 hasMany 作为链接(这可以在客户端完成),当它构建 url 时,它将预先添加 post 的 url,给你 post/1/comments
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'
}
}
}
这是一个带有 colors
和 items
Here's a dinky example with colors
and items
http://emberjs.jsbin.com/OxIDiVU/68/edit
这篇关于ember 数据适配器中的动态段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!