Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars passing object into helper

Currently I have an Ember object that looks like this:

name: 'Bob'
xs: {
    'actual':50
    'target':55
}

I have around 5-6 fields similar to xs. I need a helper method that can take that xs object and then return whether or not the target has been hit.

I thought of doing this:

Handlebars.registerHelper('hasHitTarget', function(attribute) {
    if (attribute.actual >= attribute.target)
    {
        return block(this);
    }
});

{{#each user in App.userController}}
    {{#hasHitTarget user.xs}}
        Target Hit
    {{/hasHitTarget}}
{{/each}}

Everything I've read online says this should work. But it doesn't. When I console.log(attribute) it returns user.xs as a string. What's going on?

like image 266
andy Avatar asked Nov 04 '22 02:11

andy


1 Answers

There's a difference between Handlebars & Ember.Handlebars, Ember extends Handlebars internally to add extra functionality.

That being said you are using the wrong helper here, you need to use Ember.Handlebars.registerBoundHelper.

Ember.Handlebars.registerBoundHelper('hasHitTarget', function(attribute) {
  if (attribute.actual >= attribute.target) {
    return block(this);
  }
});
like image 90
Bradley Priest Avatar answered Nov 08 '22 08:11

Bradley Priest