繁体   English   中英

在 Ember 中以编程方式调用 Handlebars 助手

[英]Calling Handlebars helper programmatically in Ember

我的一个组件需要根据提供的参数动态选择一个 Handlebar 助手。

类似的东西{{dynamic-output value=value helper=helper}}

在组件类中,我想根据提供的帮助程序输出值。

我找不到关于以编程方式使用 Handlebars 助手的太多信息:(

基本上,如果您有一个名为selectBoxOptions的助手,则可以在助手中使用以下代码来调用该助手:

return Handlebars.helpers.selectBoxOptions(selectedValue, options);

有关更多详细信息,请参阅此博客文章: http : //www.remwebdevelopment.com/blog/javascript/handlebars-calling-a-helper-function-from-another-helper-231.html

这很容易做到。 就是这样:

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 个辅助函数和 3 个辅助函数。 您现在可以按如下方式调用这些助手中的每一个:

{{helperOne someValue}}

{{helperTwo someValue}}

{{helperDynamic someValue someType}}

暂无
暂无

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

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