我正在创建消息列表,显示消息发送的时间.
这是我的短信集
Messages = new Mongo.Collection('messages');
Messages.attachSchema(new SimpleSchema({
created: {
type: Date
},
text: {
type: String
}
}));
Run Code Online (Sandbox Code Playgroud)
这是我的布局
{{#each messages}}
<li class="message">
<span class="message-text">{{text}}</span>
<span class="message-date">{{timeAgo created}}</span>
</li>
{{/each}}
Run Code Online (Sandbox Code Playgroud)
这是我的帮手
UI.registerHelper('timeAgo', function (context, options) {
if (context) {
return moment(context).fromNow();
}
});
Run Code Online (Sandbox Code Playgroud)
我怎样才能让我的助手每分钟更新一次? 除非我输入新消息或刷新页面,否则它现在不会被动.
UPDATE
Meteor-livestap就是这样做的.
将助手更改为:
Template.registerHelper('timeAgo', function (context, options) {
Session.get("time");
if (context) {
return moment(context).fromNow();
}
});
Meteor.setInterval(function() {
Session.set("time", new Date().getTime());
}, 60000);
Run Code Online (Sandbox Code Playgroud)
这样做是Session.get("time")
每分钟都在改变,并迫使助手重新计算.这应该确保时间每分钟都保持反应.