Helper loses parent context when wrapped in a custom block

139 Views Asked by At

I have a block of Handlebars that was rendering fine until I wrapped it in a custom block. Specifically there is a call to the parent context to get the Currency type. Bear in mind that this Handlebars block is wrapped in an each:

{{#each this.SubscriptionOptions.MonthlySubscriptions}}

So clearly I know where the problem is, I'm just not sure how to solve it.

Here is the block of Handlebars:

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

The piece that is now failing is this:

{{currency ../Currency}}

Here is the ifGreaterThanZero helper code:

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

I took a look at this, and it is in fact the subscription option itself, so it contains the PricePerBillingPeriod for example.

The question is, how do I get it so that reaching the parent context works again?

1

There are 1 best solutions below

0
On BEST ANSWER

Okay, so I figured it out. You have to go up one more level. It's not necessarily parent context in the object sense, it's helper parent context and so the deeper you go, it's moving up the stack like a directory structure.

The new code looks like this:

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