Reputation: 380
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>
the 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;
}
});
}
Upvotes: 0
Views: 896
Reputation: 19544
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();
...
},
});
Upvotes: 4