简体   繁体   English

如何从服务器获取嵌套路由余烬2.3.0的数据

[英]How to fetch data from server for a nested route ember 2.3.0

I have a post model and a comments model that are related as shown below: 我有一个相关的帖子模型和评论模型,如下所示:

//  app/models/post.js

import DS from 'ember-data';

export default DS.Model.extend({
 //....
 comments: DS.hasMany('comment'),
 //....
})

// app/models/comment.js

import DS from 'ember-data'

export default DS.Model.extend({
 //....
 ad: DS.belongsTo('post'),,
 //....
})

In app/router.js I have app/router.js我有

Router.map(function() {
  this.route('post', {path: '/post/:post_id'}, function() {
    this.route('comments');
  });
)};

I have a post template in app/templates/post.hbs that looks like this: 我在app/templates/post.hbs中有一个post模板,看起来像这样:

<h2>{{model.title}}</h2>
<p>{{model.body}}</p>

  {{#link-to "post.comments"}}
    <h2>Comments</h2>
  {{/link-to}}
  {{outlet}}

I also have a template to render the comments in app/templates/post/comments.hbs 我也有一个模板,可在app/templates/post/comments.hbs呈现app/templates/post/comments.hbs

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

I want when someone clicks on the comments link on the post template, the comments template be rendered on the {{outlet}} of the post template. 我想当有人单击帖子模板上的评论链接时,将评论模板呈现在帖子模板的{{outlet}}上。 The problem I have is fetching the data for comments model. 我遇到的问题是获取comments模型的数据。 I have a route for the comments in app/routes/post/comments.js and it looks like this: 我在app/routes/post/comments.js有一条comments app/routes/post/comments.js ,它看起来像这样:

import Ember from 'ember';

export default Ember.Route.extend({
  model(){
    //I don't know what to do here.
  }
});

The comments in my backend server are exposed on /posts/:post_id/comments endpoint. 我的后端服务器中的注释在/posts/:post_id/comments端点上公开。 How can i do to get the comments on the comments template? 我该如何获取评论模板上的评论?

Since you already have the Post loaded in the parent route:post , your route:post.comments can simply ask for the Comment s from it: 由于您已经在父route:post加载了Post ,因此route:post.comments可以简单地route:post.comments请求Comment

// app/routes/post/comments.js
import Ember from 'ember';

export default Ember.Route.extend({
  model(){
    const post = this.modelFor('post');
    return post.get('comments');
  }
});

I was finally able to make this work. 我终于能够完成这项工作。 With the help of James answer, The only thing that I had to do was to set a dynamic namespace for the CommentAdapter . 在James的帮助下,我唯一要做的就是为CommentAdapter设置动态名称空间。 I generated a comment adapter using the ember generator and changed my app/routes/post/comments.js file to look like this: 我使用ember生成器生成了一个comment适配器,并将我的app/routes/post/comments.js文件更改为如下所示:

// app/routes/post/comments.js
import Ember from 'ember';

export default Ember.Route.extend({
  model(){
      const post = this.modelFor('post');
      this.store.adapterFor('comment').set('namespace', `posts/${post.id}`);
      return this.store.query('comment', post.id);
    }
 }); 

This works as expected. 这按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM