把手传递给助手 [英] Handlebars passing object into helper

查看:80
本文介绍了把手传递给助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个Ember对象,如下所示:

Currently I have an Ember object that looks like this:

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

我有大约5-6个字段,类似于 xs 。我需要一个可以使用xs对象的帮助方法,然后返回目标是否被命中。

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.

我想到这样做:

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

我在线阅读的所有内容都说这个 / em>工作。但是没有。当我 console.log(attribute)它返回 user.xs 作为一个字符串。发生什么事?

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?

推荐答案

Handlebars& Ember.Handlebars,Ember在内部扩展了Handlebars以添加额外的功能。

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

据说你在这里使用了错误的帮助器,你需要使用 Ember .Handlebars.registerBoundHelper

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

这篇关于把手传递给助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆