Dmitro
Dmitro

Reputation: 1549

How to compare values in each?

I found compare helper for handlebars https://gist.github.com/doginthehat/1890659. And I try to use it like that:

{{#each item in model.records}}
<div>
    {{#compare model.currentUser.id item.poster_id}}
        <div class="author current">{{model.currentUser.name}}</div>
    {{else}}
        <div class="author remote">{{model.remoteUser.name}}</div>
    {{/compare}}
    <div class="td message-text">
        {{item.poster_id}}{{item.text}}
    </div>
</div>
{{/each}}

But it doesn't work. Because helper doesn't recognize second argument as integer value. Helper gets only text 'item.poster_id'. Also I tried to get value in helper with this.get(rvalue). But it still didn't work. I read a lot of issues about it but I can't find solution. Please advise how to compare values in each block? Or maybe I need to use another approach.

Upvotes: 1

Views: 204

Answers (1)

thecodejack
thecodejack

Reputation: 13379

There is a problem in the gist. You need to extract the lvalue and rvalue which were missing in the gist.

        lvalue = Ember.Handlebars.get(this,lvalue, options);
        rvalue = Ember.Handlebars.get(this,rvalue, options);
        var result = operators[operator](lvalue,rvalue);

Here is the updated gist. https://gist.github.com/thecodejack/a8de97f4ec410ea23722

Here is the working jsbin http://emberjs.jsbin.com/dolive/1/edit

Update: btw lvalue and rvalue correction is only for emberjs.

Explanation: Basically since you are using registerHelper, parameters are passed as string values rather than parsing them as variables. So when you pass variables var1 , var2(from my jsbin link) they are considered as strings "var1" and "var2" rather values(1,"1").

So inorder to fetch their values Ember provides a Handlebars.get() method which basically gets the value based on key string, context provided. Docs link http://emberjs.com/api/classes/Ember.Handlebars.html#method_get

But if you use registerBoundHelper we don't need Ember.Handlebars.get(), since params itself will have values passed rather key strings. Docs link http://emberjs.com/api/classes/Ember.Handlebars.html#method_registerBoundHelper

Upvotes: 5

Related Questions