Meteor.js中的Simple Clock或如何重新加载模板? [英] Simple Clock in Meteor.js or how to reload Templates?
问题描述
我正在尝试使用meteor.js中的模板,但是如何在给定的时间后重新加载它们?例如,我想有一个名为 time的模板。必须每秒钟重新加载以显示当前时间,还需要将模板日期
我当前的代码,因为它必须每分钟重新加载一次,因为日期可能每分钟更改一次,或者日历必须每5分钟左右重新加载.ics一次。
I'm trying with Templates in meteor.js, but how can I reload them after a given time? For example I want to have a Template called "time" which has to reload every second to show the current time, also a template "date" which has to reload every minute, because every minute the date can change, or an calendar which has to reload the .ics every 5 minute or so, to be always accurate.
我当前的代码看起来像这样:
my current code looks like that:
<body>
<!-- TOP -->
<div class="top left">{{> time }}</div>
<div class="top mid"></div>
<div class="top right">{{> date }}</div>
</body>
<template name="time">
<div>{{ clock }}</div>
</template>
<template name="date">
<div>{{ date }}</div>
</template>
js:
if (Meteor.isClient) {
Template.time.helpers({
clock: function () {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
return currentHours + ":" + currentMinutes + ":" + currentSeconds;
}
});
Tempate.date.helpers({
date: function () {
var month = ["Januar", "Februar", "März", "April", "Mai", "Juni", "July", "August", "September", "October", "November", "Dezember"];
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
return d + " " + month[m] + " " + y;
}
});
}
推荐答案
最简单重新加载模板的方法是提供和更改反应性数据源。在您的情况下,这可能是时钟值。
The simplest way to reload a template is to provide and change a reactive data source. In your case, this could be the clock value.
var clock = new ReactiveVar(new Date());
Template.time.onCreated(function() {
Meteor.setInterval(function() {
clock.set(new Date());
}, 1000);
});
Template.time.helpers({
clock: function() {
var currentTime = clock.get();
...
},
});
这篇关于Meteor.js中的Simple Clock或如何重新加载模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!