0

I want to create eq helper. It already exists in ember-truth-helpers addon, but I need only eq helper so I decided create it by myself in my plugin.

I've created file assets/javascripts/discourse/helpers/eq.js.es6 in my plugin with such content:

import { registerHelper } from 'discourse/lib/helpers';

registerHelper('eq', function(params) {
  return params[0] === params[1];
});

and use it in template in this way:

{{#if (eq param1 param2)}} <h1>hello</h1> {{/if}}

But eq is not defined.

What is the right way to create helper?

megas
  • 21,401
  • 12
  • 79
  • 130

2 Answers2

5

It looks like you are using ember-cli, if so kindly do go through generators in ember-cli guide

ember g helper is-equal

will result in app/helpers/is-equal.js which will initially be

import Ember from 'ember';

export function isEqual(params/*, hash*/) {
  return params;
}

export default Ember.Helper.helper(isEqual);

but you can change it to

export function isEqual([leftSide, rightSide, isCaseInsensitive]) {
  let ret;
  if (isCaseInsensitive) {
    ret = (leftSide.toLowerCase() === rightSide.toLowerCase());
  } else {
    ret = (leftSide === rightSide);
  }
  return ret;
}

export default Ember.Helper.helper(isEqual);

Now you can use it in your templates as

{{#if (is-equal 'abc' 'ABC' true)}}

{{/if}}
wallop
  • 2,510
  • 1
  • 22
  • 39
0

The problem was with the bounding. This code works for me:

import { registerHelper } from 'discourse/lib/helpers';

var makeBoundHelper = Ember.HTMLBars.makeBoundHelper;

registerHelper('eq', makeBoundHelper(function(params) {
  return params[0] === params[1];
}));

The solution is taken from here

megas
  • 21,401
  • 12
  • 79
  • 130