简体   繁体   English

在Ember中编写车把帮手

[英]Writing a handlebars helper in Ember

In my app I am trying to write a custom handlebars helper. 在我的应用程序中,我试图编写一个自定义的车把助手。 My helper looks like this 我的助手看起来像这样

Ember.Handlebars.helper('__', function(person) {
   console.log(person);
   return person.get('name');
}, 'name');

I have a person class like this 我有一个像这样的人

App.Person = Ember.Object.extend({
  name : 'moshin'
});

In my hbs file I am trying and doing this 在我的HBS文件中,我正在尝试执行此操作

{{__ person}}

But it outputs undefined. 但是它输出未定义。 Can anybody explains how to go about it? 有人可以解释如何做吗?

Is person actually defined in your template? 实际在模板中定义了人员吗? Your logic there is spot on. 您的逻辑存在。 I'm guessing maybe you've failed to create an instance of person, and maybe you aren't referencing it in the template correctly. 我猜想您可能无法创建person的实例,也许您没有在模板中正确引用它。 Here's an example of your code, working... 这是您的代码示例,正在运行...

http://emberjs.jsbin.com/fuwahemi/1/edit http://emberjs.jsbin.com/fuwahemi/1/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM