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

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

问题描述

我的应用程序后端有几个资源.为每个资源公开一个模型.

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

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

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 来获取属于某个 BlogPostComment 属于某个 User.

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

据我所知,如果我为 Comment 定义一个典型的 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 中,我有以下内容:

and in the CommentRoute I have the following:

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

请求发送到:

/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-data 可能没有这样的功能(或者在其中一些线程中声称它是这样的)时,它们中的大多数都快一年了.

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-data 是否有这样的功能.

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

类似问题:

  1. Ember 数据嵌套模型
  2. 加载嵌套资源的规范方式
  3. 深度嵌套路由

推荐答案

处理它的最佳方法是使用链接.如果你不想那样做,它远不受支持,而且很难入侵(管道只是不容易传递信息).我个人建议在这种情况下滚动您自己的适配器(Ember without Ember Data).

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天全站免登陆