ember是否具有{{itemView}}助手,可以与{{#collection}}一起使用



实际上,我正在寻找使视图负责标记集合视图中的第一个或最后一个项目的方法,我找到了这个方法,我如何在元素之间添加一个分离器在{{#each}}}循环中,除了最后一个元素之后吗?。

{{#collection contentBinding="dataList" tagName="ol" classNames="list" itemViewClass="ItemView"}}
    {{#itemView classNames="item" classNamesBinding="isLastItem:last"}}
        item templates
    {{/itemView}}
{{/collection}}

尽管可以通过另一种方式解决这,但我只想知道我是否可以找到内置的支持。我确实搜索了很长时间,只是找不到Ember.Handlebars.collection的文档,它不在最新的API文档中。

从emberjs的主版本开始,您可以尝试使用自定义车把"绑定"助手"。看起来像这样:

{{#each item in collection}}
    {{#isLastElement item}}
        template if last
    {{else}}
        template if not
    {{/isLastElement}}    
    both templates
{{/each}}

Ember.Handlebars.registerBoundHelper('islastElement', 
    function(item, options) {
     if(functionTellingIfThisIsLastElement(item))
          return options.fn(this);
     else
          return options.inverse(this);
    }
);

这只是查看事物的另一种方式,使用 functionTellingThisislAstelement 在收藏中查看或项目中的属性。要访问集合,您必须在此处更改一些内容,以获取良好的上下文 参数,具体取决于您的代码。

最新更新