使用手柄模板中的动态索引获取数组的值

时间:2014-03-14 07:38:09

标签: handlebars.js marionette

我是Handlebars的新手。

我创建了一个在CompositeView中使用的ItemView。此模板的值正确呈现。

var singleMonth = Marionette.ItemView.extend({
    tagName: 'tr',
    template: {
      type: 'handlebars',
      template: monthTemplate
    },
    months: [ 'JAN','FEB','MAR','APR','JUN','JUL','AUG','SEP','OCT','NOV','DEC' ],
    templateHelpers: function() {
      var helpers = {};
      helpers.months = this.months;
      return helpers;
    }
  });

这是我的模板

<td>{{ months.@index.[7] }}</td><td>{{ [12] }}</td>

我希望根据[7]的值获得相应的月份值,这将是月份数组的索引。

for Ex. if [7] is 3 then I want to get expression value as 'MAR'

我无法理解如何做到这一点。

你能告诉我怎么办?

(注意:我不想在这里使用#each或任何循环)

由于

1 个答案:

答案 0 :(得分:1)

尽管模板中可能存在语法错误,但模板本身不应该那么聪明。

一样保持愚蠢
<td>{{ thisMonth }}</td>

然后在thisMonth

中构建templateHelpers
templateHelpers: function() {
  // The reason for _this is: `this` means model in templateHelpers.
  _this = this; 
  return {
    thisMonth: _this.months[7]
  }
};