Denzo
Denzo

Reputation: 431

Calling Handlebars helper programmatically in Ember

One of my components needs to pick a Handlebar helper dynamically based on the provided parameter.

Something like that {{dynamic-output value=value helper=helper}}

Inside the component class I would like to output the value based on the provided helper.

I couldn't find much info on using Handlebars helpers programmatically :(

Upvotes: 2

Views: 925

Answers (2)

Jake Haller-Roby
Jake Haller-Roby

Reputation: 6427

This is pretty easy to do. Here's how:

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);
    }
});

In the above code, we've set up 2 helper functions and 3 helpers. You can now call each of these helpers as follows:

{{helperOne someValue}}

{{helperTwo someValue}}

{{helperDynamic someValue someType}}

Upvotes: 1

Trevor
Trevor

Reputation: 2308

Basically, if you have a helper called selectBoxOptions the following code can be used within a helper to call that helper:

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

See this blog post for more details: http://www.remwebdevelopment.com/blog/javascript/handlebars-calling-a-helper-function-from-another-helper-231.html

Upvotes: 2

Related Questions