mustache.js日期格式 [英] mustache.js date formatting

查看:336
本文介绍了mustache.js日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用mustache.js,到目前为止,我印象深刻.虽然有两件事使我感到困惑.第一个导致第二个,所以请耐心等待.

I have started using mustache.js and so far I am very impressed. Although two things puzzle me. The first leads on to the second so bear with me.

我的JSON

{"goalsCollection": [
    {
        "Id": "d5dce10e-513c-449d-8e34-8fe771fa464a",
        "Description": "Multum",
        "TargetAmount": 2935.9,
        "TargetDate": "/Date(1558998000000)/"
    },
    {
        "Id": "eac65501-21f5-f831-fb07-dcfead50d1d9",
        "Description": "quad nomen",
        "TargetAmount": 6976.12,
        "TargetDate": "/Date(1606953600000)/"
    }
]};

我的处理功能

function renderInvestmentGoals(collection) {
    var tpl = '{{#goalsCollection}}<tr><td>{{Description}}</td><td>{{TargetAmount}}</td><td>{{TargetDate}}</td></tr>{{/goalsCollection}}';
    $('#tblGoals tbody').html('').html(Mustache.to_html(tpl, collection));
}

第一季度 如您所见,"TargetDate"需要解析,但是我不确定如何在当前函数中执行该操作.

Q1 As you can see my 'TargetDate' needs parsing but I am unsure of how to do that within my current function.

第二季度 假设我想在渲染之前对一个或多个对象执行某些功能或格式化,那么最好的方法是什么?

Q2 Say I wanted to perform some function or formatting on one or more of my objects before rendering, what is the best way of doing it?

推荐答案

由于其在客户端/服务器之间共享的能力,我也一直在使用Mustache.我最终要做的是在服务器端将所有值(日期,货币)格式化为字符串,因此我不必依赖于辅助Javascript函数.但是,如果您要针对客户端的这些值进行逻辑处理,那么这可能对您来说效果不佳.

I've been using Mustache for my projects as well, due to its ability to be shared across client/server. What I ended up doing was formatting all values (dates, currency) to strings server-side, so I don't have to rely on helper Javascript functions. This may not work well for you though, if you're doing logic against these values client-side.

您可能还想研究使用 handlebars.js ,它本质上是Mustache,但扩展名可能会有所帮助带有客户端格式(以及更多).这里的损失是,如果这对您来说很重要,您将可能找不到服务器端实现的把手.

You might also want to look into using handlebars.js, which is essentially Mustache, but with extensions that may help with client-side formatting (and more). The loss here is that you will probably not be able to find a server-side implementation of handlebars, if that matters to you.

这篇关于mustache.js日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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