Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

javascript - Loading relationship model in 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')

I can create a comment for a particular "post" using

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

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

What is the right way to fetch the comments for a particular post id from the store and server?

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.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}}

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})
  },
});

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}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...