简体   繁体   English

把手@first不适用于Ember.js 1.0.0

[英]Handlebars @first not working with Ember.js 1.0.0

The version of Ember I am using uses a version of Handlebars that, in its release notes, claim to support @index and @first. 我使用的Ember版本使用的是Handlebars版本,在其发行说明中声称支持@index和@first。

However, when I use these in my hbs files, I get the following error: 但是,当我在hbs文件中使用它们时,出现以下错误:

SyntaxError: Unexpected token ,

Now, with a little bit of research, for the @index portion, 现在,通过一些研究,对于@index部分,

{{_view.contentIndex}}

seems to render the index. 似乎呈现索引。 However, I cannot use these in conditionals. 但是,我不能在条件中使用这些。 What I need is a working version of : 我需要的是一个工作版本:

{{#each array}}
 {{#if @first }}
  <!-- first element specific html -->
 {{else}}
  <!-- other html -->
 {{/if}}
{{/each}}

I have tried _view.first and also tried to use _view.contentIndex in a helper, but failed. 我尝试了_view.first,也尝试在辅助程序中使用_view.contentIndex,但是失败了。 Is there a workaround to this? 有没有解决方法?

Edit: the version of ember being used is v1.0.0 编辑:正在使用的余烬的版本是v1.0.0

Please stick to Ember guides instead of Handlebars guides. 请坚持使用Ember指南,而不是把手指南。 Ember uses now it's own version of Handlebars - HTMLBars and it has many differences. Ember现在使用它是Handlebars的自己的版本-HTMLBars,并且有很多区别。 That's why you can't say: 这就是为什么你不能说:

The version of Ember I am using uses a version of Handlebars 我正在使用的Ember版本使用的是Handlebars版本

At least if you use recent versions. 至少如果您使用最新版本。

You could however access first element of array in different way: 但是,您可以通过不同的方式访问数组的第一个元素:

array.[0]

You could also use index: 您还可以使用索引:

{{#each array as |item index|}}
  {{index}} is index of current element
{{/each}}

You could also mimic behavior of @first using ember-truth-helpers addon and taking advantage of eq helper - thanks to kristjan reinhold for this idea: 你也可以模仿的行为@first使用烬,真助手插件和利用eq帮手-感谢克里斯蒂安·雷纳德这个想法:

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

I made a workaround by registereing an Ember Helper, ultimately. 我最终通过注册一个Ember Helper进行了变通。

/* Attempt to add a first to each class. */
Ember.Handlebars.registerHelper('ifFirst', function(value, options) {
    var context = (options.fn.contexts && options.fn.contexts[0]) || this;
    var currentIndex = options.data.view.contentIndex;
    if (currentIndex == 0){
        // return true
        return options.fn(this);
    } else {
        // return false
        return options.inverse(this);
    }
});

This might be overkill, but the idea is that if you since contentIndex already contains the index of the array, you simply check if it equals 0. Could be used to check for the last item as well, as long as you make sure to check against the array's length. 这可能是矫kill过正,但是您的想法是,由于contentIndex已经包含数组的索引,因此您只需检查其是否等于0。只要确保检查,就可以用于检查最后一项。反对数组的长度。

Of course, I agree with the previous answers that an upgrade to a newer version of Ember is definitely better when possible, but for anyone who is stuck like I was right now, I hope this helps. 当然,我同意先前的回答,即在可能的情况下升级到较新版本的Ember绝对会更好,但是对于那些像我现在这样陷入困境的人,我希望这会有所帮助。

This solution works without any helpers. 此解决方案无需任何帮助即可工作。

{{#each items as |item notFirst|}}
   {{#if notFirst}}
      // handle second to last item here
   {{else}}
      // handle first item of array here
   {{/if}}
{{/each}}

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

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