【问题标题】:Accessing the current model within a yield在产量内访问当前模型
【发布时间】:2015-07-03 06:35:39
【问题描述】:

我正在寻找如何通过 yield 语句访问当前模型。有什么想法吗?

型号

models: [{
   id: 1,
   name:'Red',
   value: '#ff0000'
}, {
  id: 2,
  name:'Yellow',
  value: '#ffff00'
}, {
  id: 3,
  name:'Blue',
  value: '#0000ff'
}];

模板

{{#table-list models=models config=colorModelTableListConfig}}
  {{model.id}}
  {{model.name}}
  {{model.value}}
{{/table-list}}

组件(表格列表)

<!-- Searching markup-->
<table>
  {{#each th in config.tableHeaders}}
    <th>{{th}}</th>
  {{/each}}

  {{#each model in models}}
    {{yield}}
  {{/each}}
</table>
<!-- Pagination markup-->

旁注我不能将 each 放入 yield,这会导致我的搜索、分页和其他功能出现问题

回答

Ember yield param passing

【问题讨论】:

    标签: ember.js handlebars.js


    【解决方案1】:

    您可以将参数传递给 yield 块:

    {{yield model}}
    

    如有必要,您可以调整为不同的名称:

    {{#with model as foobar}}
      {{yield foobar}}
    {{/with}}
    

    你可以使用 each 助手做类似的事情:

    {{#each model in models as |foobar|}}
      {{yield foobar}}
    {{/each}}
    

    更多示例:https://github.com/emberjs/ember.js/blob/06e41ad7ccd28558dd4e651aa070bc06f7757821/packages/ember-htmlbars/tests/integration/block_params_test.js

    【讨论】:

    • 我在表格列表组件中尝试过{{yield model}}{{yield model=model}},但无法访问model.name
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多