如何在Hogan.js中使用Lambda时获取值 [英] How to get the value when using Lambda in Hogan.js

查看:62
本文介绍了如何在Hogan.js中使用Lambda时获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下函数在jQuery中处理AJAX成功回调:

 函数成功(数据){
var templateData = {
items:data,
formatMoney:function(){
return function(value){
return Globalization.format(value,'N');
};
}
};

//填充报表表
var filledTable = tableTemplate.render(templateData);
$ tableContainer.html(filledTable);
}

以下是我的模板的样子:

  {#Items}} 
< tr>
< td> {{ProductId}}< / td>
< td> {{#formamaMoney}} {{Cost}} {{/ formatMoney}}< / td>
< / tr>
{{/ Items}}

问题在于代替成本I的值得到{{费用}}。我知道这应该是如何工作的,因为它在胡子手册(http://mustache.github.com/mustache.5.html)中有描述,但我想要获得价值。



这个会指向我的产品对象,所以我可以使用 this.Cost 获得成本但是这是一个简单的案例,我有很多具有许多需要格式化的属性的对象类型,因此我需要一个通用的解决方案来保存事物



我也可以在服务器端计算这个,但我更喜欢在客户端这样做我不仅将这些数据用于hogan,还用于客户端的其他计算。



是否有更直接,通用和客户端方式来获取Cost的值而不是未渲染的文字块?

解决方案

是的。



小胡子的实现严格遵守规范是非常有限的。您的选项将在您的lambda代码中呈现{{Cost}}字符串(您应该将此字符串和渲染函数作为您的lambda参数),并将渲染后的字符串解析为浮点数,即成本。 / p>

肯定不干净。但它会奏效。不要忘记在正在使用的Mustache实现的存储库中打开一个问题。


I have the following function which handles AJAX success callback in jQuery:

function success(data) {
    var templateData = {
        items: data,
        formatMoney: function () {
            return function (value) {
                return Globalization.format(value, 'N');
            };
        }
    };

    // fill reports table
    var filledTable = tableTemplate.render(templateData);
    $tableContainer.html(filledTable);
}

Here's how my template looks like:

{#Items}}
<tr>
    <td>{{ProductId}}</td>
    <td>{{#formatMoney}}{{Cost}}{{/formatMoney}}</td>
</tr>
{{/Items}}

The problem is that instead of the value of Cost I get {{Cost}}. I know this is how it should work as it's described in mustache manual (http://mustache.github.com/mustache.5.html) but I'd like to get the value instead.

this would point to my product object so I could get cost using this.Cost but this is for a simple case and I have many object types with many properties that need formatting therefore I need a generic solution to keep things DRY.

I could also calculate this on the server side but I prefer to do this on the client side since I am not only using this data with hogan but also for other calculations on the client side.

Is there more straightfoward, generic and client side way to get the value of Cost instead of the unrendered literal block?

解决方案

Yep.

Mustache implementations that scrictly adhere to the spec are very limiting. Your option would be, in your lambda code, to render the "{{Cost}}" string (you should get this string and a rendering function as your lambda parameters), and parse the rendered string into a float, your cost.

Not clean, for sure. But it would work. Don't forget to open an issue in the repository of the Mustache implementation you are using.

这篇关于如何在Hogan.js中使用Lambda时获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆