【问题标题】:How to compare values in each?如何比较每个值?
【发布时间】:2014-08-05 09:45:23
【问题描述】:

我找到了车把的比较助手https://gist.github.com/doginthehat/1890659。 我尝试这样使用它:

{{#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}}

但它不起作用。因为助手不将第二个参数识别为整数值。助手仅获取文本“item.poster_id”。我还尝试使用 this.get(rvalue) 在助手中获得价值。但它仍然没有工作。我阅读了很多关于它的问题,但我找不到解决方案。 请告知如何比较每个块中的值?或者也许我需要使用另一种方法。

【问题讨论】:

    标签: javascript ember.js handlebars.js


    【解决方案1】:

    主旨有问题。您需要提取 gist 中缺少的左值和右值。

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

    这是更新的要点。 https://gist.github.com/thecodejack/a8de97f4ec410ea23722

    这是工作的 jsbin http://emberjs.jsbin.com/dolive/1/edit

    更新:顺便说一句,lvaluervalue 更正仅适用于 emberjs。

    说明:基本上由于您使用的是registerHelper,因此参数作为字符串值传递,而不是作为变量解析。因此,当您传递变量 var1var2(来自我的 jsbin 链接)时,它们被视为字符串“var1”和“var2”而不是值(1,“1”)。

    因此,为了获取它们的值,Ember 提供了一个Handlebars.get() 方法,该方法基本上基于提供的上下文的键字符串获取值。文档链接http://emberjs.com/api/classes/Ember.Handlebars.html#method_get

    但是如果你使用registerBoundHelper,我们就不需要Ember.Handlebars.get(),因为参数本身会传递值而不是键字符串。文档链接http://emberjs.com/api/classes/Ember.Handlebars.html#method_registerBoundHelper

    【讨论】:

    • 是的,它的作品。但我不明白它是如何工作的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    相关资源
    最近更新 更多