提问者:小点点

enpe-cli支持每个助手中的Handlebar@vars(即@index、@key、@first、@last)


每当我有一个在每个助手内部使用@vars变量(即@index、@key、@first、@last)的Handelbar模板时,我都会在enpe-cli中得到一个编译错误。(参见每个助手内部这些@vars变量的留档http://handlebarsjs.com/#iteration。)下面是一个使用enpe-cli构建的简单应用程序,其中只包含两个添加到程序中的文件:路由/application. js和模板/application.hbs。在这篇文章的底部是enpe-cli给出的编译错误消息的屏幕截图。我的代码中有错误吗?或者这是我应该在github@上报告的bughttps://github.com/stefanpenner/ember-cli?

路由/应用程序. js

export default Ember.Route.extend({
    model: function() {
        return ['red', 'blue', 'green'];
    }
});

模板/应用程序. hbs

{{#each model}}
  {{@index}}: {{this}}
{{/each}}

以下是所涉及的各种工具的版本:

  • enpe-cli: 0.0.40
  • 节点:0.10.30
  • npm: 1.4.21
  • 车把:1.3.0
  • 余烬: 1.6.1

共2个答案

匿名用户

这真的与enber-cli无关。Ember Handlebar不支持@关键字项。

匿名用户

可以模仿以下Handlebar关键字的行为:@index@key@first@last

{{#each array as |item index|}}
  Index of item: `{{item}}` is: `{{index}}`
{{/each}}
{{#each-in object as |key value|}}
  {{key}}: {{value}}
{{/each-in}}

您还可以使用enber-Truth-help ers插件并利用eqhelper来模仿@first的行为-感谢kristjan重新持有这个想法:

{{#each array as |item index|}}
  {{#if (eq index 0)}}
    <!-- first element specific html -->
  {{else}}
    <!-- other html -->
  {{/if}}
{{/each}}

您可以使用(eq item array. firstObject)而不是(eq index 0)

正如dwickern建议的那样,您可以使用Ember. Array.lastObject来模仿@last行为。

{{#each array as |item|}}
  {{#if (eq item array.lastObject)}}
    <!-- last element specific html -->
  {{else}}
    <!-- other html -->
  {{/if}}
{{/each}}