Javascript 在Ember中以编程方式调用Handlebar助手
我的一个组件需要根据提供的参数动态拾取把手辅助对象 类似于Javascript 在Ember中以编程方式调用Handlebar助手,javascript,ember.js,handlebars.js,Javascript,Ember.js,Handlebars.js,我的一个组件需要根据提供的参数动态拾取把手辅助对象 类似于{{dynamic output value=value helper=helper}} 在component类中,我希望根据提供的帮助器输出值 我找不到太多关于以编程方式使用把手助手的信息:(这很容易做到。以下是如何: helperFunctionOne(value){ //Fill with the data manipulation you want for "helperOne" } helperFunctionTwo(
{{dynamic output value=value helper=helper}}
在component类中,我希望根据提供的帮助器输出值
我找不到太多关于以编程方式使用把手助手的信息:(这很容易做到。以下是如何:
helperFunctionOne(value){
//Fill with the data manipulation you want for "helperOne"
}
helperFunctionTwo(value){
//Fill with the data manipulation you want for "helperTwo"
}
Ember.Handlebars.registerBoundHelper("helperOne", function(value){
return helperFunctionOne(value);
});
Ember.Handlebars.registerBoundHelper("helperTwo", function(value){
return helperFunctionTwo(value);
});
Ember.Handlebars.registerBoundHelper("helperDynamic", function(value, type){
if(type == 1){
return helperFunctionOne(value);
else if(type == 2){
return helperFunctionTwo(value);
}
});
在上面的代码中,我们设置了2个helper函数和3个helper。您现在可以按如下方式调用每个helper:
{{helperOne someValue}}
{{helperTwo someValue}}
{{helperDynamic someValue someType}}
基本上,如果您有一个名为
selectBoxOptions
的助手,则可以在助手中使用以下代码来调用该助手:
return handlebar.helpers.selectBoxOptions(selectedValue,options);
有关更多详细信息,请参阅此博客帖子:您有多少不同的参数?您可以将此逻辑嵌套在更多的句柄中,如
{{{if hasparetera}{{{dynamic output value=value helper=helper}{{/if}}
我相信您误解了这个问题。我的组件采用了两个参数“value”和“helper”。组件的输出是应用了helper的值。“helper”的示例是什么?helper=handlebar.helper例如,我有一个helper,它可以将日期对象转换为漂亮的日期,或将数字格式转换为美元值。这是一个很好的解决方法。但是,我想知道如何调用实际的帮助器(“helperOne”或“helperTwo”),而不是调用帮助器正在使用的函数。您实际上是在要求“嵌套”把手帮助器,但这(不幸)不受支持。嵌套帮助器是可能的。请参阅: