When I load the page all is working fine, but: why momentjs doesn't update the time?
If I insert a new post is inserted, momentjs doesn't update the a few seconds ago text with a minute ago.
This is what I'm doing.
I've added the momentjs:moment
package
Registered the helper
UI.registerHelper('timeAgo', function(datetime) {
return moment(datetime).fromNow();
});
In the Template
<template name="postItem">
<li>
{{text}} - {{timeAgo createdAt}}
</li>
{{/if}}
</template>
The output is
Hi! - a few seconds ago
Hello! - 23 minutes ago
The date isn't a reactive variable so it won't refresh when the time difference changes.
You can make it reactive by forcing it to recalculate every minute (or a custom interval):
UI.registerHelper('timeAgo', function(datetime) {
Session.get('time');
return moment(datetime).fromNow();
});
setInterval(function() {
Session.set("time", new Date())
}, 60000); //Every minute
This will force the helper to redraw the value for time every minute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With