2016-05-03 40 views
0

我想僅顯示問卷模板一段時間。從5月5日到5月9日。流星 - 在特定時間段內解鎖模板(按日期)

我想像一個模板助手,如果5月5日開始,返回true,如果9月5日已過,則返回false。但是,我怎樣纔能有效地更新if語句的瞬時時間值呢?

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

感謝您的幫助!

莫夫

回答

1

您可以使用自動運行時間分配給一個變量的反應,如會話父模板中的變量或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; 
     } 
    } 
}) 

希望有所幫助。

+0

謝謝 - 但是當使用setInterval方法時,我在控制檯中得到以下錯誤,並且時間沒有更新:'setInterval回調中的異常:TypeError:func不是函數' – Raggamuffin

+0

明白了:setInterval需要一個函數作爲第一個值,在一個匿名函數中使用Session.set工作:'Meteor.setInterval(()=> {Session.set(TIME_NOW,new Date())},6000);' – Raggamuffin

相關問題