簡體   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