垃圾数据适配器中的动态段 [英] Dynamic segment in ember data adapter

查看:61
本文介绍了垃圾数据适配器中的动态段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个可以检索不受我控制的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 。但是我想为我的评论 -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?

推荐答案

修改你的帖子json以将hasMany作为链接(这可以在客户端完成)它会建立一个url,它会添加该帖子的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'
    }
  }
}

这是一个简洁的例子,颜色项目

Here's a dinky example with colors and items

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

这篇关于垃圾数据适配器中的动态段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆