-1

I am using Ember with Handlebars template

How can I get the following support in Handlebars ?

{{#if response.someCount> 0}}

Would I have to add some mapping attribute in Controller to achieve this ?

copenndthagen
  • 49,230
  • 102
  • 290
  • 442

1 Answers1

0

You need helper for that, for example:

Handlebars.registerHelper('compare', function (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);
    }
  });

And then in Your template:

{{#compare response.someCount 0 operator = ">"}}
// Some code...
{{/compare}}

Sorry can't remember from where I toot that code, but it runs flawlessly in one project for over the year.

Bogdan Kuštan
  • 5,427
  • 1
  • 21
  • 30