1

实际上,我正在寻找让 View 负责标记集合视图中的第一个或最后一个项目的方法,我发现了这个How do I add a separator between elements between an {{#each}} loop except after最后一个元素? .但我不想在javascript中定义itemViewClass模板的attrs(例如classNames),我可以只使用{{itemView}}助手或其他东西来定义itemView模板吗?

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

虽然,这可以通过其他方式解决,但我只想知道是否可以找到内置支持。而且我确实搜索了很长时间,只是找不到Ember.Handlebars.collection's 的文档,它不在最新的 API 文档中。

4

1 回答 1

0

从 Emberjs 的主版本开始,您可以尝试使用自定义 Handlebars “绑定”助手。它看起来像这样:

{{#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);
    }
);

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

于 2012-12-21T08:19:16.453 回答