Trouble adding Ember helper

AI-generated summary

In a discussion about troubleshooting an Ember helper, user stevenpslade reported an error where the helper named ‘color-shade’ could not be found. User eviltrout suggested that the file extension was incorrect, recommending the use of .js.es6 to ensure the file is autoloaded. After making this change, stevenpslade resolved the missing helper error but encountered a new TypeError regarding Ember.Helper.helper. eviltrout explained that the code was using a newer Ember API incompatible with Discourse’s older version and advised referencing existing Discourse helper definitions instead. Following this guidance, stevenpslade successfully implemented the helper using the registerUnbound method, resolving the issue.

Continuing the discussion from How to add an Ember js helper?:

I am trying to add an Ember helper but having no luck. I get this error:

Uncaught Error: Assertion Failed: A helper named 'color-shade' could not be found

Below is my helper, color-shade.js …

import Ember from 'ember';

export function colorShade(params) {
  //just placeholder code for now
  return params[0] === params[1];
}

export default Ember.Helper.helper(colorShade);

Where did you put the helper? The location of the file matters.

I am defining the helper in:

assets/javascripts/discourse/helpers/color-shade.js

I am using the helper for a plugin outlet:

assets/javascripts/discourse/components/connectors/topic-category/topic-header.hbs

Try the extension .js.es6 – otherwise it’s not autoloaded.

Nice, good news is that A helper named 'color-shade' could not be found is no longer occurring.

Bad news is that I am now getting the error: Uncaught TypeError: Cannot read property 'helper' of undefined in regards to:

export default Ember.Helper.helper(colorShade);

Do I need to import Ember.Helper?

I think that’s the newer ember api. We’re on an older version. I’d suggest just looking at how the discourse helpers are defined and copying that.

:ok_hand: Bingo! Defining my helper the following way did the trick:

import registerUnbound from 'discourse/helpers/register-unbound';

registerUnbound('color-shade', function(params) {
  console.log(params);
});

Thanks!