[英]Handlebars helper not getting value of variable
I'm Trying to create a custom "if_eq" helper for a Handlebars / Ember application. 我正在尝试为Handlebars / Ember应用程序创建一个自定义的“ if_eq”帮助程序。 The helper is called with the following syntax: {{#if_eq item.name 'bob'}}, but is receiving 2 string values to compare (literally 'item.name' and 'bob'), instead of the value of "item.name" in the context it was called. 使用以下语法调用该帮助程序:{{#if_eq item.name'bob'}},但是正在接收2个要比较的字符串值(字面上是'item.name'和'bob'),而不是“ item”的值.name”。 Wondering what I might be doing wrong. 想知道我可能做错了什么。 Thanks! 谢谢!
Relevant code snippets are listed below and also created a jsbin to illustrate the problem here 相关的代码片段如下所列又创造了jsbin来说明这个问题在这里
Helper Code 助手代码
Handlebars.registerHelper('if_eq', function(a, b, opts) {
console.log( "Comparing ", a, b);
if(a == b)
return opts.fn(this);
else
return opts.inverse(this);
});
Template code 模板代码
<ul>
{{#each item in model}}
{{#if_eq item.name 'bob'}}
<li>We have a bob here!</option>
{{else}}
<li>A non-bob</li>
{{/if_eq}}
{{/each}}
</ul>
Handlebars helpers don't play nice with bound properties. 车把帮手的绑定属性不能很好地发挥作用。
Generally you'll want to avoid such logic in your templates. 通常,您需要在模板中避免这种逻辑。 Specifically, either create a method on your controller, or the object itself, so that you say something like: if item.isBob
. 具体来说,要么在控制器上创建一个方法,要么在对象本身上创建一个,这样您就可以说: if item.isBob
。
Eg http://emberjs.jsbin.com/fudadugemu/2/edit?html,js,output 例如http://emberjs.jsbin.com/fudadugemu/2/edit?html,js,输出
Or you could use a component and have a little fun with it, something like this: 或者您可以使用一个组件并对其进行一些有趣的操作,例如:
http://emberjs.jsbin.com/buyeqenumo/2/edit http://emberjs.jsbin.com/buyeqenumo/2/edit
(try changing the bob input) (尝试更改鲍勃输入)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.