在Ember数据中加载关系模型 [英] Loading relationship model in Ember data

查看:59
本文介绍了在Ember数据中加载关系模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Ember Data存有疑问.我有一个帖子和评论模型.

I have certain doubts with respect to Ember Data. I have a post and comments model.

//post.js
comments: DS.hasMany('comment')

//blog.js
post: DS.belongsTo('post')

我可以使用

let blogPost = this.get('store').findRecord('post', 1);
let comment = this.get('store').createRecord('comment', {
   post: blogPost
});

但是我如何获取特定帖子的所有评论?就像,我有多个帖子,其中包含很多评论,我希望针对特定帖子ID的所有评论,例如posts/id/comments

But how can i fetch all the comments for a particular post? Like, I have multiple posts which has many comments and i want all the comments for a particular post id, say posts/id/comments

从商店和服务器中获取特定帖子ID的评论的正确方法是什么?

我得到的服务器响应只是"findRecord"发布时评论的ID.我遵循的是REST API格式,并使用REST适配器和REST序列化程序进行自定义.

The server response i get is only the id's of the comments while "findRecord"ing a post. I am following the REST API format and using REST Adapter and REST serializer for customizations.

谢谢.

推荐答案

有很多方法可以将这些记录加载到存储中.您如何选择它取决于后端的设置方式.最简单的选项是使用模板中的子记录,或进行查询.以下说明仅适用于使用REST适配器的应用程序(因为JSONAPI请求已标准化).

There are a many ways to get these records loaded into the store. How you choose to do it depends on how the back end is set up. The easiest options are using the child records in a template, or doing a query. The instructions below only apply to apps using a REST adapter (since JSONAPI requests are standardized).

让我们从使用模板中的记录开始.

Let's start with using the records in a template.

在路线中,获取帖子并将其返回到模型挂钩中:

In the route, fetch the post and return it in the model hook:

// post.js route    
import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.findRecord('post', params.post_id)
  },
});

将模型及其子记录传递给组件:

Pass the model and its child records to a component:

// post.hbs route template
{{some-component comments=model.comments}}

然后使用该组件中的注释:

Then use the comments in that component:

{{#each comments as |comment|}}
   <p>{{comment.note}}<p>
{{/each}}

您应该发现,在模板中使用注释会触发GET请求,这些请求会随您的初始帖子提取操作返回,例如/comments/1 /comments/2 等.在组件的操作中,您可以作为 this.get('comments')

You should find that using the comments in a template triggers GET requests to the ids that came back with your initial post fetch, such as /comments/1, /comments/2, etc. In the component's actions, you can access the comments as this.get('comments')

另一种选择是指定后端要接收和处理的查询参数.

Another option is to specify a query parameter to be received and processed by the back end.

// post.js route    
import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.query('comments', {post: params.post_id})
  },
});

上面的代码将向/comments?post = 1发出GET请求.后端负责过滤数据库并仅返回属于该帖子的记录.由于模型是注释的集合,因此您可以在这样的模板中使用上述结果:

The code above will make a GET request to /comments?post=1. The back end is responsible for filtering through the database and returning only the records that belong to the post. Since model is a collection of comments, you'd use the results of the above in a template like this:

{{#each model as |comment|}}
  <p>{{comment.note}}</p>
{{/each}}

这篇关于在Ember数据中加载关系模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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