Ember CLI - How to use a util function in a template helper function?


#1

What am I doing wrong here?

  • I define a util function isXDefactoEqualY.
  • I use this util function to define a template helper isXEqualY.
  • When using this template helper in a specific template, then this template is NOT rendered.
  • I see no compilation errors.
  • I see no errors in console.

app/utils/is-x-defacto-equal-y.js

import Ember from 'ember';

export default function ([model, list]) {
    ... code
    return true;
}

app/helpers/is-x-equal-y.js

import Ember from 'ember';
import isXDefactoEqualY from '../utils/is-x-defacto-equal-y';

export function isXEqualY([model, list]) {
    return isXDefactoEqualY (model, list);
}

export default Ember.Helper.helper(isXEqualY);

app/templates/x.hbs

<div>isXEqualY: {{isXEqualY myModel myList}}</div>

#2

Helper names are dasherized in your template. Should be {{is-x-equal-y}}


#3

Dasherizing (kebab-case) does not make a difference - I tried both :slight_smile: I have other not-based-on-a-util-function template helpers up running. For those helpers both {{kebab-case …} and {{camelCase …}} works fine.


#4

isXDefactoEqualY is called with individual arguments instead of an array, resulting in:

Uncaught TypeError: Invalid attempt to destructure non-iterable instanceon line 3 ofapp/utils/is-x-defacto-equal-y.js`

I would change that function definition to:

export default function (model, list) { ... }

I also think the camelCase helper call is an issue, but maybe that’s environment-specific. Take a look at this twiddle to see what I’m talking about.


#5

@maffews, you are so spot-on!

Ember passes an array of arguments to a helper function!

The problem is, that I forgot to remove the array notation from the parameter list, when I copied from the original helper code.

My code now works perfect! Thank you @maffews! Case closed :slight_smile:


#6

@maffews, I saw your twiddle. Yes, to avoid problems with different environments, it is probably best always to use kebab-case notation. Thank you!