2014-02-14 45 views
2

我的組件之一需要根據提供的參數動態地選擇Handlebar幫助器。在Ember中以編程方式調用把手助手

類似的東西{{dynamic-output value=value helper=helper}}

組件類裏面我想輸出基礎上提供幫助的價值。

我不能使用把手助手程序:(找到很多信息

+1

有多少不同的參數,你呢?你總是可以將這個邏輯嵌套在更多的句柄中,比如'{{#if hasParameterA}} {{dynamic-output value = value helper = helper}} {{/ if}}' – alalani

+0

我相信你誤解了這個問題。我的組件需要兩個參數'value'和'helper'。組件的輸出是應用助手的值。 – Denzo

+0

什麼是'助手'的例子? – chopper

回答

1

這是很容易做到的方法如下:。

helperFunctionOne(value){ 
    //Fill with the data manipulation you want for "helperOne" 
} 

helperFunctionTwo(value){ 
    //Fill with the data manipulation you want for "helperTwo" 
} 

Ember.Handlebars.regsiterBoundHelper("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}} 
+0

這是一個偉大的工作。但是,我想知道如何調用實際的幫助器('helperOne'或'helperTwo')來反對調用幫助器正在使用的函數。 – Denzo

+0

然後你基本上要求「嵌套」手柄助手,並且(不幸的是)不支持。 – gravityplanx

+0

嵌套助手是可能的。請參閱:http://www.remwebdevelopment.com/blog/javascript/handlebars-calling-a-helper-function-from-another-helper-231.html – Trevor