Ember:从每个帮助程序中访问控制器属性

时间:2014-11-05 22:57:06

标签: ember.js conditional handlebars.js

我的控制器上有一个属性,我们称之为showTheStuff,我的模板中有一个{{#each}}帮助器。现在,我只是明确地设置属性。

在该列表中,我想根据showTheStuff的值有条件地呈现或不呈现某些标记,但如果我使用{{#unless}}条件,则它不起作用。

http://emberjs.jsbin.com/jarapabela/1/

我如何让它工作? 在我的代码中,它不适用于{{#unless}},但如果我切换属性并使用{{#if}},它确实有效。还有其他人经历过这个吗?

我正在使用Ember 1.6。

控制器:

export default Ember.ArrayController.extend({
  hideDeleteButton: true,
  actions: {
    deleteComment: function(comment) {
      comment.destroyRecord();
      this.removeObject(comment);
    }
  }
});

模板: 模板是在另一个模板中呈现的模态组件。打开它的{{#link-to}}通过传递一组注释传入。该属性在{{#each}}之外生效,但在其中必须是未定义的,因为即使尝试在第一个单元格中输出它,渲染时也不会显示任何内容。

  <table class="table table-striped table-condensed">
    <thead>
      <tr>
        <th>Comments</th>
        {{#unless hideDeleteButton}}
          <th></th>
        {{/unless}}
      </tr>
    </thead>
    <tbody>
      {{#each}}
        <tr>
          <td>
            {{hideDeleteButton}}
            <br>
            {{comment}}
          </td>
          <td>
            {{createdByResource}}{{hideDeleteButton}}
          </td>

          {{#unless hideDeleteButton}}
            <td class="text-center">
              {{confirm-delete-button action="deleteComment" param=this}}
            </td>
          {{/unless}}
        </tr>
      {{/each}}
    </tbody>
  </table>

2 个答案:

答案 0 :(得分:2)

@ Kingpin2k让我朝着正确的方向前进,而jsbin我认为并不完全符合我的实际代码。问题是我的hideDeleteButton属性实际上并不知道,因为它迭代传入的模型。

但是,如果我在引用属性时指定controller,它就可以工作!

{{#unless controller.hideDeleteButton}}
  <td class="text-center col-md-1">
    {confirm-delete-button action="deleteComment" param=this}}
  </td>
{{/unless}}

答案 1 :(得分:1)

确保为{{#each}}块提供一些上下文。

示例:{{#each object in model}}或新语法:{{#each model as | object |}}

这应该允许你引用hideDeleteButton而不使用&#34; controller.hideDeleteButton&#34;

我假设hideDeleteButton是您控制器上的属性,其余属于您模型的属性:

{{#each model as |object|}}
  <tr>
    <td>
      {{hideDeleteButton}}
      <br>
      {{object.comment}}
    </td>
    <td>
      {{object.createdByResource}}{{hideDeleteButton}}
    </td>

    {{#unless hideDeleteButton}}
      <td class="text-center">
        {{confirm-delete-button action="deleteComment" param=object}}
      </td>
    {{/unless}}
  </tr>
{{/each}}
相关问题