Handlebars passing object into helper

别等时光非礼了梦想. 提交于 2019-12-06 02:37:17

问题


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?


回答1:


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);
  }
});



回答2:


Passing an object to a Handlebars Helper from within an #each doesn't work as intended, due to a bug in Ember (currently v1.0), there is a workaround though ... see my post here

https://stackoverflow.com/a/18787740/1780102



来源:https://stackoverflow.com/questions/15338671/handlebars-passing-object-into-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!