Meteor-在一段时间内解锁模板(按日期)

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

本文关键字:日期 一段时间 解锁 Meteor-      更新时间:2023-09-26

我只想显示一段时间的问卷模板。从5月5日到5月9日。

我想象一个类似模板助手的东西,如果5月5日已经开始,它会返回true;如果5月9日已经过去,它就会返回false。但是如何有效地更新if语句的瞬时时间值呢?

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

谢谢你的帮助!

Muff

您可以使用autorun将时间分配给父模板中的响应变量,如Session变量或ReactiveVar。

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

希望能有所帮助。