如何使用嵌套资源使用Ember数据 [英] How to use Ember Data with Nested Resources

查看:81
本文介绍了如何使用嵌套资源使用Ember数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用后端有几个资源。一个模型是为每个资源公开的。

My application backend has several resources. A model is exposed for each resource.

所有其他资源的入口点是通过 User 模型。我的意思是,给定一个用户,我们可以找到 BlogPost 。给定一个 BlogPost ,我们可以找到评论等。

The entry point to all other resources is through the User model. What I mean is, given a User we can find BlogPost. Given a BlogPost we can find Comments etc.

在Ember术语中,我们可以说:

In Ember terminology, we could say:

User hasMany BlogPost
BlogPost hasMany Comment
Comment belongsTo BlogPost

通过后端公开REST形式的API:

By backend exposes REST APIs of the form:

GET /api/v1/users/1
GET /api/v1/users/1/blog_posts/1
GET /api/v1/users/1/blog_posts/1/comments/1

我试图找出如何使用Ember Data fetch 评论属于一定的 BlogPost 属于某个用户

I'm trying to figure out how to use Ember Data to fetch Comment belonging to a certain BlogPost belonging to a certain User.

从我看到的,如果我为评论定义了一个典型的Ember模型:

From what I see, if I define a typical Ember model for Comment:

App.Comment = DS.Model.extend({
  ...
  blogPost: DS.belongsTo('App.BlogPost')
});

CommentRoute 我有以下:

var CommentRoute = MessageRoute.extend({
    model: function(params) {
        this.store.find('comment')
    },

请求发送到: / p>

The request is sent to:

/api/v1/comments

我甚至不知道从哪里开始,以便Ember Data使用以下格式的URL:

I don't even know where to begin in order for Ember Data to use urls of the form:

GET /api/v1/users/1/blog_posts/1/comments/1

已经看到几个类似的问题(见下面的链接),但是还没有看到任何一个确切的答案,他们大多数几乎是一年前,当ember数据可能没有这样的功能在某些线程中声明)

I've seen several similar questions asked (see links below), but haven't seen a definitive answer to any of them. Most of them are almost a year old when ember-data, perhaps, did not have such functionality (or so it is claimed in some of these threads).

我再次要求确认ember数据是否具有此类功能。

I ask again to confirm whether ember-data has such functionality or not.

类似的问题:


  1. Ember数据嵌套模型

  2. 加载嵌套资源的规范方式

  3. 深层嵌套的路线

  1. Ember Data nested Models
  2. Canonical way to load nested resources
  3. Deep nested routes


推荐答案

处理它的最好方法是链接。如果你不想这样做,它远远没有得到支持,并且难以入侵(管道不容易传递信息)。就个人而言,我建议您在这种情况下滚动自己的适配器(没有Ember数据的Ember数据)。

The best way to handle it is with links. If you don't want to do it like that, it is far from supported, and difficult to hack in (the pipeline just doesn't easily pass the information around). Personally I'd recommend rolling your own adapter in that case (Ember without Ember Data).

App.Foo = DS.Model.extend({
  name: DS.attr('string'),
  bars : DS.hasMany('bar', {async:true})
});

App.Bar = DS.Model.extend({
  foo: DS.belongsTo('foo'),
});

json:

{
  id: 1,
  name: "bill",
  links: {
    bars: '/foo/1/bars'
  }
}

示例: http://emberjs.jsbin.com/OxIDiVU/971/edit

这篇关于如何使用嵌套资源使用Ember数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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