当包装在自定义块中时,助手会丢失父上下文

Helper loses parent context when wrapped in a custom block

我有一个车把块,在我将它包裹在一个自定义块中之前,它呈现得很好。具体来说,调用父上下文以获取 Currency 类型。请记住,此 Handlebars 块包含在 each:

{{#each this.SubscriptionOptions.MonthlySubscriptions}}

我很清楚问题出在哪里,只是不知道如何解决。

这是把手块:

<p class="lead" style="font-size:40px">
  {{#ifGreaterThanZero PricePerBillingPeriod}}
    <strong>{{currency ../Currency}}{{priceFormat PricePerBillingPeriod}}</strong>
  {{else}}
    <strong>FREE</strong>
  {{/ifGreaterThanZero}}
</p>

现在失败的作品是:

{{currency ../Currency}}

这里是 ifGreaterThanZero 辅助代码:

Handlebars.registerHelper('ifGreaterThanZero', function(value, options) {
  var intVal = parseInt(value);
  if (intVal) {
    return options.fn(this);
  } else {
    options.inverse(this);
  }
});

我看了一下this,实际上是订阅选项本身,所以它包含了例如PricePerBillingPeriod

问题是,我如何获取它以便再次访问父上下文?

好的,我明白了。你必须再升一级。它不一定是对象意义上的 parent context,它是 helper parent context 所以你走得越深,它就像目录结构一样向上移动堆栈.

新代码如下所示:

<strong>{{currency ../../Currency}}{{priceFormat PricePerBillingPeriod}}</strong>