Meteor Handlebars:{{#each}} 块中数组中的值

Meteor Handlebars: value in array in {{#each}} block

我是 Meteor/Handlebars 使用包含这两个助手的流星包 https://atmospherejs.com/aldeed/plans-stripe 的新手。

AppPlans.listDefined() - to get the list of all our plans 
 - ["free", "monthly", "yearly"]

AppPlans.get() - to see which plan the user currently has. 
 - ["free"]

https://github.com/raix/Meteor-handlebar-helpers 有这个有用的助手:

{{$eq a b}} if a equals b then return true

我正在 HTML 中展示计划,如下所示:

{{#each AppPlans.listDefined}}

   <strong>{{this}}</strong>

{{/each}}

但我想做的是这样的事情;这不起作用

{{#each AppPlans.listDefined}}
   {{#if $eq AppPlans.listDefined AppPlans.get}}
        <strong>{{this}}</strong>
   {{else}}
      <span>{{this}}</span>
   {{/if}}
{{/each}}

解决此问题的最佳方法是什么?

您当前比较的是两个数组,而不是两个标量。 $eq 是行不通的。由于您正在遍历计划,因此您实际上想查看当前(标量)计划 this 是否在当前用户拥有的计划 数组 中。

{{#each AppPlans.listDefined}}
   {{#if $inArray this AppPlans.get}}
        <strong>{{this}}</strong>
   {{else}}
      <span>{{this}}</span>
   {{/if}}
{{/each}}

然后 $inArray 帮手:

Template.registerHelper('$inArray',function(s,a){
  return a.indexOf(s) > -1;
});