Search code examples
javascriptmeteormeteor-blaze

Meteor - Unlock template for a certain period of time (by Date)


I'd like to show a Questionaire-Template for a period of time only. For from May the 5th until May the 9th.

I imagine something like a template helper that returns true if May the 5th has started and false if May the 9th has passed. But how can i efficiently update the momentary time value for the if-statement?

Template.registerHelper("questionaireUnlocked", function() {
  let now = new Date();
  let startDate;
  let endDate;
  if (now > startDate && now < endDate) {
    return true;
  }
});

Thanks for your help!

Muff


Solution

  • You can use autorun to assign time to a reactive variable such as Session variable or ReactiveVar in the parent template.

    Template.myParentTemplateName.created = function () {
      const template = this;
      template.autorun(function () {
         Meteor.setInterval(Session.set("newTime",new Date()), 6000);         
      });
    }
    
    Template.myParentTemplateName.helpers ({
      "questionaireUnlocked" : function () {
          const now = Session.get("newTime");
          .....
          if (now > startDate && now < endDate) {
              return true;
          }
       }
    })
    

    Hope that helps.