简体   繁体   English

由两个分组的Ember.js对象的车把模板

[英]Handlebars template for Ember.js objects grouped by two

I have my data in the collection and I need to render them into the template. 我的数据集中在集合中,我需要将它们渲染到模板中。 How can I achieve the result when the data is grouped by two? 将数据分为两部分时如何获得结果? I need to sort them like this: 我需要像这样对它们进行排序:

._______________________
| 1 | 3 | 5
|___|___|_______________
| 2 | 4 | and so on...
|___|___|_______________

I have created vertical div element for each 1+2, 3+4, ... pair to style the items like this. 我已经为每个1 + 2、3 + 4,...对创建了垂直div元素,以对此类项进行样式设置。 How can I render the data in to such grid with handlebars? 如何使用手把渲染到这样的网格中? All I can do is this: 我所能做的就是:

{{#each App.myController}}
 ... render item ...
{{/each}}

Firstly, i'd attempt to do this in CSS if at all possible in your layout. 首先,如果可能的话,我会尝试在CSS中执行此操作。

If not, your best bet would to add a computed property to your controller that groups them into pairs and do it that way. 如果不是,最好的选择是将计算属性添加到控制器中,然后将它们成对分组,并以此方式进行操作。 Something like this: 像这样:

{{#each App.myController.pairedContent}}
  <!-- view for content.firstObject -->
  <!-- view for content.lastObject -->
{{/each}}

App.MyController = Ember.ArrayController.extend({
  pairedContent: ( function(){
    return this.get('content').reduce( function(array, object, index){
      if(index % 2){
        array.get('lastObject').pushObject(object)
      }
      else{
        array.pushObject(Ember.A([object]))
      }
      return array
    }, Ember.A())
  }).property('content')
})

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

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