Momentjs does not update .fromNow () time

When I load the page, everything works fine, but: why doesn't momentjs update the time?
If I insert a new record, it adds a minute ago a few minutes ago.

This is what I do.

I added a package momentjs:moment

Register Assistant

UI.registerHelper('timeAgo', function(datetime) {
    return moment(datetime).fromNow();
});

In the template

<template name="postItem">
    <li>
      {{text}} - {{timeAgo createdAt}}
    </li>
  {{/if}}
</template>

Output signal

Hi! - a few seconds ago
Hello! - 23 minutes ago    
+4
source share
3 answers

The date is not a reactive variable, so it is not updated when the time difference changes.

You can make it reactive by forcing it to recount every minute (or user 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 cause the assistant to redraw the time value every minute.

+6
source

http://docs.meteor.com/#/full/reactivity

These Meteor functions run your code as a reactive computation:

Templates
Tracker.autorun
Blaze.render and Blaze.renderWithData


And the reactive data sources that can trigger changes are:

Session variables
Database queries on Collections
Meteor.status
The ready() method on a subscription handle
Meteor.user
Meteor.userId
Meteor.loggingIn



In addition, the following functions which return an object with a stop method, if called from a reactive computation, are stopped when the computation is rerun or stopped:

Tracker.autorun (nested)
Meteor.subscribe
observe() and observeChanges() on cursors

(),

, ,

, .

, http://docs.meteor.com/#/full/reactivevar_pkg

https://atmospherejs.com/copleykj/livestamp

, . .

+2

, . , . - . . :

0
source

Source: https://habr.com/ru/post/1569208/


All Articles