Moments in meteor reactivity?

Used https://github.com/acreeger/meteor-moment in the meteor, and it works well, however there is a way to make the moment output reactive so that it counts "3 seconds ago", "4 seconds ago", etc.

+6
source share
4 answers

Thanks for the answers to everyone, I found the mrt package that does the work of atmospherejs.com/package/livestamp

+1
source

Instead of using a new session variable for each individual timer, I would create one Tracker.Dependency , which is marked for changes every second (or maybe every 10 seconds), and then depends on it when you want to depend on the current time .

 var timeTick = new Tracker.Dependency(); Meteor.setInterval(function () { timeTick.changed(); }, 1000); fromNowReactive = function (mmt) { timeTick.depend(); return mmt.fromNow(); } Template.example.helpers({ example: function () { return fromNowReactive(moment().startOf('hour')); } }); 

This is the approach taken by mizzao:timesync , which is a useful package that you can use if those fromNow side timestamps. One reason not to use timestamps generated by the client is that they may not be synchronized, which results in lines like 5 seconds from now for the message that has just been made. mizzao:timesync allows you to use omnidirectional server timestamps everywhere, as well as effectively group different reactivity intervals.

+10
source

Now you can use the copleykj: livestamp package . ( github atmosphere )

Install it as follows:

 meteor add copleykj:livestamp 

He has a dependence on the moment: the moment, so this will automatically lead to this. It installs a universal helper available anywhere, and a date object can be passed.

You can use it in a template as follows:

 <li>Regular: {{date}} </li> <li>Livestamp: {{livestamp date}}</li> 

Here's a working demo in MeteorPad

+2
source

Use setTimeout and Session to store your variable.

Something like this (in your lib file):

 var updateTime = function () { var time = moment().startOf('hour').fromNow(); // 22 minutes ago Session.set('timeFromNow', time); setTimeout(updateTime, 60 * 1000); // 60 seconds }); updateTime(); 

Then in your template:

 Template.t.timeFromNow = function () { return Session.get('timeFromNow'); } 

When setTimeout is started to update the Session variable, the template is updated.

0
source

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


All Articles