简体   繁体   English

ember-cli在javascript中调用makeBoundHelper函数

[英]ember-cli call a makeBoundHelper function in javascript

I have an ember-cli helper function: translate-i18n that I am nicely using in my hbs templates as an accessory helper to dockyards i18n translation library. 我有一个ember-cli helper函数:translation-i18n,我很高兴在我的hbs模板中使用它作为船坞i18n翻译库的辅助助手。

The helper I created takes the word as an argument and just provides for a few fallback situations (ex if a translation is missing, it will fire an airbrake error). 我创建的帮助程序将单词作为参数,并且仅提供了一些后备情况(例如,如果缺少翻译,则将引发空刹车错误)。

My helper is working fantastically when used within hbs templates, but it seems like it is impossible to call it from a JS file, specifically another helper file. 在hbs模板中使用我的帮助程序时,它的工作异常出色,但是似乎无法从JS文件(特别是另一个帮助程序文件)中调用它。 In a nutshell, the problem is this: 简而言之,问题是这样的:

  • If I create the helper using Ember.Handlebars.makeBoundHelper(translate); 如果我使用Ember.Handlebars.makeBoundHelper(translate);创建帮助器Ember.Handlebars.makeBoundHelper(translate); the helper is not accessible using Ember.Handlebars.helpers .... so I can't call it that way. 使用Ember.Handlebars.helpers ...无法访问该帮助程序,所以我不能这样称呼它。 I CAN see that the helper exisits by doing this.container.lookup('helper:translate-i18n'), but I don't seem to have the ability to call the function this way. 我可以通过执行this.container.lookup('helper:translate-i18n')看到该助手已经退出,但是我似乎没有这种方式调用该函数的能力。
  • If I create the helper using register (instead of makeBound), so by doing this: export default Ember.Handlebars.registerBoundHelper('translate', translate()); 如果我使用寄存器(而不是makeBound)创建帮助器,请执行以下操作: export default Ember.Handlebars.registerBoundHelper('translate', translate()); I can then access my helper from anywhere, but suddenly the container is not defined, and I no longer can get at the dockyards i18n (t helper), as this line throws an error: var t = this.container.lookup('utils:t'); 然后,我可以从任何地方访问我的助手,但是突然没有定义容器,并且我不再可以到达码头i18n(t助手),因为此行引发错误: var t = this.container.lookup('utils:t');

Any suggestions/pointers/observations would be greatly appreciated! 任何建议/指针/观察将不胜感激!

If you have a handlebar helpers registered then you can access them from JS like 如果您已经注册了车把帮手,则可以从JS访问它们,例如

For eg : If you have a helper named ' doSomething ', 例如:如果您有一个名为“ doSomething ”的助手,

Ember.Handlebars.registerBoundHelper('doSomething', function (args) {
 return something;
});

You can call the helper from JS like the below 您可以像下面这样从JS调用帮助程序

Ember.Handlebars.helpers.doSomething(args);

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

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