【问题标题】:How to render into parent resource's index {{outlet}}如何渲染到父资源的索引 {{outlet}}
【发布时间】:2013-06-23 07:23:27
【问题描述】:

我有 2 个嵌套资源,在 Ember 路由器中发布和评论。我希望这反映网址:

/posts/1/comments/1

去这个页面应该,

  1. 使用帖子/索引模板呈现 id = 1 的帖子。
  2. 使用评论/索引模板为评论 id = 1 的帖子呈现评论

这是jsbin上的示例。

我的路由代码是,

App.Router.map(function() {
  this.resource('home', { path: '/' });
  this.resource('posts', { path: '/posts' }, function() {
    this.resource('post', { path: ':post_id' }, function() {
      this.resource('comments', { path: 'comments' }, function() {
        this.resource('comment', { path: ':comment_id' }, function() {
          // need explicit index
        });
      });
    });
  });
});

模板和余下的 Ember 代码几乎没有库存。唯一不同的是我从主路由重定向到/posts/1/comments/1

我无法让帖子或评论在 /index 模板中呈现。帖子正文和评论正文都是空白的。

如果我将索引模板的内容嵌入到主帖子或 cmets 模板中,它会起作用。但这不是我需要的,评论需要嵌套在帖子中。

任何想法如何使它工作?谢谢。

PS:我使用的是 ember-latest 和 ember-data latest。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    通常这类问题归结为命名约定。可能 ember 正在寻找您不期望的控制器和模板。尝试将以下内容添加到您的应用程序中,然后观看控制台,它将帮助您查看正在使用的路由/控制器/模板。

    App = Ember.Application.create({
      LOG_ACTIVE_GENERATION: true,
      LOG_TRANSITIONS: true,
      LOG_VIEW_LOOKUPS: true
    });
    

    此外,这些路线可能比他们需要的更复杂。通常,将 'post' 路由嵌套在 'posts' 资源中是没有意义的。评论/cmets也是如此。试试这个:

    App.Router.map(function() {
      this.route('posts');
      this.resource('post', { path: '/posts/:post_id' }, function() {
        this.route('comments')
        this.resource('comment', { path: '/comments/:comment_id' });
      });
    });
    

    【讨论】:

    • 谢谢。减少的嵌套简化了事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多