【发布时间】:2014-04-20 16:12:51
【问题描述】:
HTML:
<div class="leaderboard">
{{#each players}}
{{> player}}
{{/each}}
</div>
<template name="player">
<div class="player {{selected}}">
<span class="name">{{name}}</span>
<span class="score">{{score}}</span>
<span class="joindate">{{joindate}}</span>
</div>
</template>
JS:
Players = new Meteor.Collection("players");
if (Meteor.isClient) {
Template.players = function () {
return Players.find();
};
}
if (Meteor.isServer) {
Meteor.startup(function () {
if (Players.find().count() === 0) {
var names = ["Ada Lovelace",
"Grace Hopper",
"Marie Curie",
"Carl Friedrich Gauss",
"Nikola Tesla",
"Claude Shannon"];
for (var i = 0; i < names.length; i++)
Players.insert({name: names[i],
score: Math.floor(Random.fraction()*10)*5,
joindate: new Date().getTime()
});
}
});
}
在上面的示例中,我将 joindate 存储为 unix 时间戳。如何在返回 Template.players 函数之前将加入日期变量格式化为 Collection.find 结果中的可读日期。
我知道我可以在插入命令中格式化日期,但我的问题是在传递给模板之前如何操作/格式化 collection.find 中的数据。
【问题讨论】:
标签: mongodb meteor handlebars.js