【问题标题】:ember - need template to show records for nested routeember - 需要模板来显示嵌套路由的记录
【发布时间】:2015-01-10 16:24:34
【问题描述】:

我似乎没有在我的路由器中正确设置我的模型挂钩,因为我的数据正在模板中呈现。但也许我还缺少其他东西

这是我的路由器

Router.map(function() {
  this.resource('movies', function() {
    this.route('show', { path: ':movie_id'}, function() {
      this.resource('rewrites', function() {
        this.route('show', { path: ':id'});
        this.route('new');
        this.route('edit');
      });
    });
    this.route('edit', { path: ':movie_id/edit'});
    this.route('new');
  });
});

在我下面的重写索引模板模板/重写/index.hbs 中,我需要显示针对一部特定电影的所有重写

{{outlet}}

{{link-to 'Add a New Rewrite' 'rewrites.new'}}

<table class="table table-bordered">
  <thead>
    <tr>
      <th>Name</th>
      <th>Script</th>
      <th>Author</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    {{#each rewrite in movie}}
      <tr>
        <td>
          {{rewrite.name}}
        </td>
        <td>{{rewrite.script}}</td>
        <td>{{rewrite.author}}</td>
        <td>{{link-to 'Edit this Rewrite' 'rewrites.edit' this}}</td>
        <td><a href="#" {{action 'delete' rewrite}}>Delete</a></td>
      </tr>
    {{/each}}
  </tbody>
</table>

我的路线中的模型设置,routes/rewrites/index.js,是

model: function() {
    var movie = this.modelFor('movies.show');
    return this.store.findAll('rewrite', { movie:movie });
  },

我已经为模型尝试了很多东西,也尝试过

return this.store.findAll('rewrite');

无论哪种方式都找到并渲染了模板,但没有数据 创建重写 2 后,我尝试过

return this.store.find('rewrite', 2);

这也不会呈现任何数据。

更新: 上面的路由器是正确的。上面的templates/rewrites/index.hbs应该是{{#each rewrite in model}} 模型钩子应该是

 model: function() {
    return this.modelFor('movies.show').get('rewrites');
  },

另外,这个应用程序的 api 是一个 rails 应用程序。在电影的 rails 序列化程序中,我需要将 has_many 添加到 sideload 重写中,如下所示:

class MovieSerializer < ActiveModel::Serializer
  attributes :id, :title, :director, :releaseDate, :cast, :description, :imageUrl
  has_many :rewrites, embed: :ids, include: true
end

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    您的each 不正确; movie 是单个对象,而不是要迭代的数组。试试这个:

    {{#each rewrite in movie.rewrites}}
    

    至少,假设您的对象模型是电影hasMany 重写。如果你真的想在路由中独立加载重写,你可以像这样迭代它们:

    {{#each rewrite in model}}
    

    【讨论】:

    • 感谢您的评论。我都试过了。它仍然没有渲染,这让我更加坚定地相信我没有正确设置模型挂钩。
    • 是的,没有看到更多代码,很难说。您是否安装了 Ember Inspector 插件?然后您可以查看模型是否正确加载。
    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多