How can I execute jet dates

I am creating a list of messages that show how long the message was sent.

Screen shot

This is my collection of messages.

Messages = new Mongo.Collection('messages');
Messages.attachSchema(new SimpleSchema({
    created: {
        type: Date
    },
    text: {
        type: String
    }

}));

this is my layout

{{#each messages}}

            <li class="message">
                    <span class="message-text">{{text}}</span>
                    <span class="message-date">{{timeAgo created}}</span>
            </li>

{{/each}}

This is my assistant

UI.registerHelper('timeAgo', function (context, options) {
    if (context) {
        return moment(context).fromNow();

    }
});

How can I make my assistant update every minute? Now it does not respond if I do not enter a new message or refresh the page.

UPDATE

Meteor-livestap does just that.

+1
source share
1 answer

Change helper:

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);

What this does is change Session.get("time")every minute and makes your assistant count. This should ensure that time remains reactive every minute.

+5
source

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


All Articles