json mustache.js 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10064975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mustache.js date formatting
提问by Dooie
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.
我已经开始使用 mustache.js,到目前为止我印象非常深刻。虽然有两件事让我感到困惑。第一个通向第二个,所以请耐心等待。
My JSON
我的 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)/"
}
]};
My handling function
我的处理功能
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));
}
Q1As you can see my 'TargetDate' needs parsing but I am unsure of how to do that within my current function.
Q1如您所见,我的“TargetDate”需要解析,但我不确定如何在当前函数中执行此操作。
Q2Say 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?
Q2假设我想在渲染之前对一个或多个对象执行一些功能或格式设置,那么最好的方法是什么?
采纳答案by mystictheory
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.
我也一直在我的项目中使用 Mustache,因为它能够在客户端/服务器之间共享。我最终做的是将所有值(日期、货币)格式化为服务器端的字符串,因此我不必依赖辅助 Javascript 函数。但是,如果您在客户端对这些值进行逻辑处理,这可能对您不起作用。
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.
您可能还想考虑使用handlebars.js,它本质上是 Mustache,但具有可能有助于客户端格式化(以及更多)的扩展。这里的损失是您可能无法找到车把的服务器端实现,如果这对您很重要的话。
回答by McGarnagle
You can use a "lambda"
您可以使用“拉姆达”
"TargetDate": "/Date(1606953600000)/",
"FormatDate": function() {
return function(rawDate) {
return rawDate.toString();
}
}, ...
Then in the markup:
然后在标记中:
<td>
{{#FormatDate}}
{{TargetDate}}
{{/FormatDate}}
</td>
From the link:
从链接:
When the value is a callable object, such as a function or lambda, the object will be invoked and passed the block of text. The text passed is the literal block, unrendered.
当值是一个可调用对象时,例如函数或 lambda,该对象将被调用并传递文本块。传递的文本是未渲染的文字块。
回答by JVitela
I have created a small extension for Mustache.js which enables the use of formatters inside of expressions, like {{expression | formatter}}
我为 Mustache.js 创建了一个小扩展,它允许在表达式中使用格式化程序,例如 {{expression | 格式化程序}}
You would anyway need to create a function that parses your date value like this:
无论如何,您都需要创建一个函数来解析您的日期值,如下所示:
Mustache.Formatters = {
date: function( str) {
var dt = new Date( parseInt( str.substr(6, str.length-8), 10));
return (dt.getDate() + "/" + (dt.getMonth() + 1) + "/" + dt.getFullYear());
}
};
And then just add the formatter to your expressions:
然后只需将格式化程序添加到您的表达式中:
{{TargetDate | date}}
You can grab the code from here: http://jvitela.github.io/mustache-wax/
你可以从这里获取代码:http: //jvitela.github.io/mustache-wax/
回答by HMR
It's a long time ago but got on this looking for exactly the same. Mustachejs (now) allows you to call functions of the passed data and not only that; in the function the value of thisis whatever value is true in a section.
这是很久以前的事了,但开始寻找完全一样的东西。Mustachejs(现在)允许您调用传递数据的函数,不仅如此;在函数中, 的值this是某个部分中为真的任何值。
If my template is like this:
如果我的模板是这样的:
{{#names}}
<p>Name is:{{name}}</p>
<!-- Comment will be removed by compileTemplates.sh
#lastLogin is an if statement if lastLogin it'll do this
^lastLogin will execute if there is not lastLogin
-->
{{#lastLogin}}
<!--
formatLogin is a method to format last Login
the function has to be part of the data sent
to the template
-->
<p>Last Login:{{formatLogin}}</p>
{{/lastLogin}}
{{^lastLogin}}
not logged in yet
{{/lastLogin}}
{{#name}}
passing name to it now:{{formatLogin}}
{{/name}}
{{/names}}
And Data like this:
和这样的数据:
var data={
names:[
{name:"Willy",lastLogin:new Date()}
],
formatLogin:function(){
//this is the lastDate used or name based on the block
//{{#name}}{{formatLogin}}{{/name}}:this is name
//{{#lastLogin}}{{formatLogin}}{{/lastLogin}}:this is lastLogin
if(!/Date\]$/.test(Object.prototype.toString.call(this))){
return "Invalid Date:"+this;
}
return this.getFullYear()
+"-"+this.getMonth()+1
+"-"+this.getDate();
}
};
var output = Mustache.render(templates.test, data);
console.log(output);
回答by jmort253
You can get the timestamp using simple String methods:
您可以使用简单的 String 方法获取时间戳:
goalsCollection.targetDate = goalsCollection.targetDate.substring(6,18);
Of course, this depends on your timestamp being the same length each time. Another option is:
当然,这取决于您的时间戳每次都具有相同的长度。另一种选择是:
goalsCollection.targetDate =
goalsCollection.targetDate.substring(6, goalsCollection.targetDate.length - 1);
These techniques aren't specific to Mustache and can be used to manipulate data for any library. See the Mozilla Developer Center Documentation on substringfor more details.
这些技术并非特定于 Mustache,可用于操作任何库的数据。有关更多详细信息,请参阅有关 substring的Mozilla 开发人员中心文档。
回答by jimmyo
To declare a function within a json you can always do this.
要在 json 中声明函数,您可以随时执行此操作。
var json = '{"RESULTS": true, "count": 1, "targetdate" : "/Date(1606953600000)/"}'
var obj = JSON.parse(json);
obj.newFunc = function (x) {
return x;
}
//OUTPUT
alert(obj.newFunc(123));

