Ember,用把手包裹cols行{{#each}}

时间:2015-03-09 04:28:37

标签: ember.js handlebars.js client-side-templating

我使用了把手{{#each}} 我需要改变这个:

<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>

进入这个:

<div class="row">
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
</div>
<div class="row">
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
</div>
<div class="row">
<div class="col-lg-4">{{somevalue}}</div>
<div class="col-lg-4">{{somevalue}}</div>
</div>

我注册了车把帮手:

Handlebars.registerHelper('isFourthOpen', function (index, opts) {
    if ((index + 1) % 3 == 1)
        return opts.fn(this);
    else
        return opts.inverse(this);
});

Handlebars.registerHelper('isFourthClose', function (index, opts) {
    if ((index + 1) % 3 == 0)
        return opts.fn(this);
    else
        return opts.inverse(this);
});

如何将当前索引传递给此帮助程序? 我试着这样做:

 {{#isFourthOpen _view.contentIndex}}

但是当我查看变量&#34; index&#34;在检查员中,我只看到&#34; _view.contentIndex&#34;作为字符串,而不是价值。

如果有人不太复杂,请告诉我。

Ember 1.9.1 把手2.0.0

1 个答案:

答案 0 :(得分:0)

我认为您最好将数据分组并使用2个{{#each}}块。

尝试这样的事情:

模板:

{{#each group in groupedItems}}
  <ul>
    {{#each item in group}}
      <li>{{item}}</li>
    {{/each}}
  </ul>
{{/each}}

控制器:

App.IndexController = Ember.Controller.extend({
  groupSize: 3,
  groupedItems: Ember.computed('items.[]', function() {
    var items = this.get('model');
    var grouped = [];
    var groupSize = this.get('groupSize');

    items.forEach(function(item, index) {
      if (index % groupSize === 0) grouped.pushObject([]);
      grouped.get('lastObject').pushObject(item);
    });

    return grouped;
  })
});

以下是一个完整的工作示例,使用自定义计算属性宏:http://emberjs.jsbin.com/rapugivavi/3/edit?html,js,output

更新

此示例包含与原始问题更相似的操作和标记。

模板:index
{{#each group in groupedItems}}
  <div class="row">
    {{#each item in group}}
      {{render 'post' item}}
    {{/each}}
  </div>
{{/each}}
模板:post
<div {{bind-attr class=":item isEditing"}}>
  {{model}}

  <div class="actions">
    {{#if isEditing}}
      <a {{action "doneEditing"}}>Done</a>
    {{else}}
      <a {{action "edit"}}>Edit</a>
    {{/if}}
  </div>
</div>