Search code examples
ember.jsember-cli

Ember CLI + HTMLBars -- Passing Parameters to a Helper


I'm trying to pass a parameter from my HTMLBars template to a Helper.

As per the documentation, I've created a helper and explicitly registered the helper:

export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
   console.log("value: "+value);
});

But I get an error "Error: Assertion Failed: makeBoundHelper generated helpers do not support use with blocks"

So I've tried using Ember.HTMLBars.helper and Ember.HTMLBars.registerHelper as suggested here but I get errors "TypeError: Ember.default.HTMLBars.helper is not a function"

If I don't reigster the helper explicitly:

export default function(value, options) {
   console.log("value: "+value);
};

Then I can pass a parameter, but it doesn't get resolved and logs out the literal text of what I passed.

So I tried the solution outlined here but it doesn't seem to work with CLI

The result I want is for a component to be dynamically selected based on the value of the parameter I send to the helper. My HTMLBars code looks like:

{{#each foo in model}}
  {{is-foo parameter}}
    {{a-component}}
  {{else}}
    {{another-component}}
  {{/is-foo}}
{{/each}}

I'm not sure what to do next. Any help is appreciated.


Solution

  • Do something like this in Ember CLI project, because helper file name is the helper name

    export default Ember.HTMLBars.makeBoundHelper(function(value, options) {
    

    instead of

    export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
    

    Edit

    Upon @TheCompiler 's request here is the suggestion as an answer.

    Do something like in HTMLBars

    {{if is_parameter}}
      {{a-component}}
    {{else}}
      {{another-component}}
    {{/if}}
    

    In Controller or Component the computed parameter property

    is_parameter: function () {
      var pm = this.get('parameter');
    
      return (your condition for `pm`)? true : false;
    }.property('parameter')