如何比较每个值? [英] How to compare values in each?
问题描述
我发现比较助手的手柄 https://gist.github.com/doginthehat/1890659。
我尝试使用它:
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}}
但它不工作。因为帮助器不能将第二个参数识别为整数值。助手只得到文本'item.poster_id'。我也试图用this.get(rvalue)获得帮助的值。但它仍然没有奏效。我读了很多关于它的问题,但我找不到解决方案。
请指教如何比较每个块中的值?或者也许我需要使用另一种方法。
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.
推荐答案
要点中有一个问题。您需要提取缺点中的左值和右值。
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);
这是更新的要点。
https://gist.github.com/thecodejack/a8de97f4ec410ea23722
这里是工作jsbin
http:/ /emberjs.jsbin.com/dolive/1/edit
Here is the working jsbin http://emberjs.jsbin.com/dolive/1/edit
更新:btw lvalue
和 rvalue
更正仅适用于emberjs。
Update: btw lvalue
and rvalue
correction is only for emberjs.
说明:基本上,由于您使用 registerHelper
,参数作为字符串值传递,而不是将其解析为变量。所以当你传递变量 var1
, var2
(从我的jsbin链接)他们被认为是字符串var1和 var2而不是值(1,1)。
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").
所以为了获取它们的值Ember提供了一个 Handlebars.get()
方法,它基本上是基于键字符串,上下文提供。文档链接 http://emberjs.com/api/classes/Ember.Handlebars.html #method_get
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
但是如果您使用 registerBoundHelper
,则不需要 Ember.Handlebars.get()
,因为params本身将具有值传递的关键字符串。文档链接 http://emberjs.com/api/classes/Ember.Handlebars.html #method_registerBoundHelper
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
这篇关于如何比较每个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!