How do I use the Ember helper


#1

I want to use the below helper

{{compare model.someCount 0 operator = ">"}}

How can I use it? I am getting "Assertion Failed: Helpers may not be used in the block form, for example {{#my-helper}}{{/my-helper}}. Please use a component, or alternatively use the helper in combination with a built-in Ember helper, for example {{#if (my-helper)}}{{/if}}."

My helper looks as below;

import Ember from 'ember';

export function compare(lvalue, rvalue, options) {
	if (arguments.length < 3)
      throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
    operator = options.hash.operator || "==";
    var operators = {
      '==': function (l, r) {
        return l == r;
      },
      '===': function (l, r) {
        return l === r;
      },
      '!=': function (l, r) {
        return l != r;
      },
      '<': function (l, r) {
        return l < r;
      },
      '>': function (l, r) {
        return l > r;
      },
      '<=': function (l, r) {
        return l <= r;
      },
      '>=': function (l, r) {
        return l >= r;
      },
      'typeof': function (l, r) {
        return typeof l == r;
      }
    }
    if (!operators[operator])
      throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
    var result = operators[operator](lvalue, rvalue);
    if (result) {
      return options.fn(this);
    } else {
      return options.inverse(this);
    }
}

export default Ember.Helper.helper(compare);

#2

You must have found an old example that used private APIs. Those don’t work anymore, and there is a clean and well-documented helper API now. http://emberjs.com/api/classes/Ember.Helper.html#method_helper

let operators = { ... same as you had ... };
export default Ember.Helper.helper(function compare([lvalue, rvalue], hash) {
    return operators[hash.operator](lvalue, rvalue);
});

Also, you may just want to add the ember-truth-helpers addon instead of creating all of these yourself.