简体   繁体   中英

How to react to two date objects in a user's profile in Meteor

In my app users have a start and end attribute in their profile (both date objects, a few hours apart). I'd like to make my app react to these values--for example, display something if the current time is between the two times, or within 15 minutes of the start time. I have a feeling I'd want to use the Tracker.autorun piece of Meteor, I'm not sure how to use it since you don't ever manually change the time, it's something that's always changing.

something like this:

timeDif : function() {
    Tracker.autorun(function() {
        var m1 = moment();
        var m2 = moment(Meteor.user().profile.time);
        var difMin = m1.diff(m2, 'minutes');
        if(difMin > 10)  {foo;}
        else {bar;}
    })

}

As you found, that won't work because Date objects are not reactive. Have a look at something like the chronos pacakge, which provides a variety of helpers for reactive dates. Based on the docs, it looks like something like this could work:

timeDiff : function() {
  var m1 = Chronos.liveMoment();
  var m2 = moment(Meteor.user().profile.time);
  var minutes = m1.diff(m2, 'minutes');
  ...
}

I'm assuming that timeDiff is a helper so you don't need the autorun . If not, let me know and I'd give some other ideas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM