1

I'm trying to pass my controller value in customized Handlebars if condition helper. But I am not able to get the value inside my helper

I am updating my controller.scope value, which comes from my ajax response. Scope will be differ upon the need as 0,1,2,3.

In my hbs,

{{#ifCond controller.scope '==' '0'}}
     //Doing something here.
{{/ifCond}}

{{#ifCond controller.scope '==' '1'}}
     //Doing something here.
{{/ifCond}}

{{#ifCond controller.scope '==' '2'}}
     //Doing something here.
{{/ifCond}}

{{#ifCond controller.scope '==' '3'}}
     //Doing something here.
{{/ifCond}}

In my helper class,

Ember.Handlebars.registerHelper('ifCond', function (temp_v1, operator, temp_v2, options) { // No I18N

var v1,v2;
v1 = Ember.Handlebars.get(this, temp_v1, options);
v2 = Ember.Handlebars.get(this, temp_v2, options);
if( v1 == undefined ){
    v1 = temp_v1;
}
if( v2 == undefined ){
    v2 = temp_v2;
}

switch (operator) {
    case '==':
        return (v1 == v2) ? options.fn(this) : options.inverse(this);
    default:
        return options.inverse(this);
}
});

console.log(v1); // It prints 'scope', but need like '0' or '1' or '2' or '3'.

My console prints always the raw value of v1, but I need the evaluate value of v1.

How to do this?

James Z
  • 12,209
  • 10
  • 24
  • 44
Priyanga V
  • 241
  • 2
  • 9
  • From some time you cannot bind properties in Em.Handlebars.registerHelper, use registerBoundHelper, please take a look at my self-answered question here: http://stackoverflow.com/questions/28558696/helper-broken-in-ember-1-10 – licancabur Mar 17 '15 at 13:41
  • @licancabur I tried what you said. I got the evaluate value of mine. But I am getting this error message **Uncaught TypeError: Cannot read property 'split' of undefined** My code example is below, **Helper Class:-** Ember.Handlebars.registerBoundHelper('isSame', function(value1, value2) { return value1 == value2 }); **Usage:-** {{#if (isSame scope "0")}}
    Hello
    {{/if}} What should I do now? Did I do any mistake here?
    – Priyanga V Mar 18 '15 at 05:23
  • @licancabur **FYI:** I am using Ember version 1.7. – Priyanga V Mar 18 '15 at 05:39
  • Oh, this solution is for 1.10 :) Sorry that I forgot to mention this. Only from 1.10 you could use bound helpers in templates like this. If you will be upgrading, be sure to read release info in for each next Ember version to make the upgrade more painless. – licancabur Mar 18 '15 at 07:32

0 Answers0