繁体   English   中英

在Ember.js中渲染hasMany数据

[英]Rendering hasMany data in Ember.js

我有一个users模型如下的应用程序-

App.User = DS.Model.extend({
  name         : DS.attr(),
  email        : DS.attr(),
  comments       : DS.hasMany('comment'),

});

App.User.FIXTURES = [{
  id: 1,
  name: 'Jane Smith',
  email: 'janesmith@thesmiths.com',
  comments: ["1", "2"]

}, {
  id: 2,
  name: 'John Dorian',
  email: 'jd@sacredheart.com',
  comments: ["1", "2"]
}

];

还有一个user模板,用于显示有关单个用户的详细信息,如下所示:

<script type = "text/x-handlebars" id = "user">
<div class="user-profile">
 {{#if deleteMode}}
            <div class="confirm-box">
                <h5>Really?</h5>
                <button {{action "confirmDelete"}}> yes </button>
                <button {{action "cancelDelete"}}> no </button>
            </div>
            {{/if}}

  <h2>{{name}}</h2>
  <span>{{email}}</span>
  <ul>
  {{#each user.comments}}
  <li>{{user.comments}}</li>
  {{/each}}
  </ul>

  <button {{action "edit"}}>Edit</button>
<button {{action "delete"}}>Delete</button>
</div>

{{outlet}}

</script>

我的问题是用户评论未显示在页面上。 谁能告诉我我做错了什么,这对Ember来说很陌生。 谢谢。

假设

App.Comment = DS.Model.extend({
  comment      : DS.attr(),
});

并且您具有夹具数据以供注释

范围发生了变化,所以this您的每个里面的评论

{{#each user.comments}}
  <li>{{comment}}</li>
  or
  <li>{{this.comment}}</li>
{{/each}}


{{#each comment in user.comments}}
  <li>{{comment.comment}}</li>
{{/each}

暂无
暂无

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

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