简体   繁体   English

如何在Hogan.js中使用Lambda时获取值

[英]How to get the value when using Lambda in Hogan.js

I have the following function which handles AJAX success callback in jQuery: 我有以下函数在jQuery中处理AJAX成功回调:

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}}. 问题是,而不是成本的价值我得{{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. 我知道它应该如何工作,因为它在胡子手册(http://mustache.github.com/mustache.5.html)中描述,但我想得到的价值。

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 . this将指向我的产品对象,所以我可以使用this.Cost得到成本,但这是一个简单的情况,我有许多对象类型,需要格式化的许多属性,因此我需要一个通用的解决方案来保持干燥

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. 我也可以在服务器端计算这个,但我更喜欢在客户端这样做,因为我不仅使用hogan这个数据,而且还用于客户端的其他计算。

Is there more straightfoward, generic and client side way to get the value of Cost instead of the unrendered literal block? 是否有更直接,通用和客户端方式来获取Cost的值而不是未渲染的文字块?

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. 您的选项将在您的lambda代码中呈现“{{Cost}}”字符串(您应该将此字符串和渲染函数作为您的lambda参数),并将渲染的字符串解析为浮点数,即成本。

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. 不要忘记在您正在使用的Mustache实现的存储库中打开一个问题。

I think we have two options. 我想我们有两种选择。

1) Use lambdas in hogan.js 1)在hogan.js中使用lambdas

res.render("template", {
          lambdas:{
            formatMoney: function( cost ){
              return cost.format('usd');
            }
          });

and the template should be 而模板应该是

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

2) As stated in the question, we can use this object. 2)如问题所述,我们可以使用这个对象。

Javascript code is Javascript代码是

res.render("template", {
  formatMoney:{
    return function(key){
      var cost = this[key];
      return cost.format('usd');
    };
  }
});

and the template is 而模板是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM