流星:设置把手变量true / false

时间:2015-11-14 17:13:45

标签: javascript variables if-statement meteor handlebars.js

我对这个整个把手概念都很陌生,但基本上我希望能够在点击按钮时将变量设置为true,然后再从提交模板中再次返回false,模板仅在显示时显示变量是真的。

对于身体,我有以下代码:

<body>
{{#if notification}} 
    {{> notifier}}
{{else}}
    {{#if currentUser}}
        {{> dashboard}}
    {{else}}
        {{> login}}
    {{/if}}
{{/if}}
</body>

假设我在仪表板中有一个链接,该链接在客户端js中运行,标识为noticationTestLink,我将在仪表板事件中添加以下函数:

'click #noticationTestLink' : function(event) {
   event.preventDefault();
}

如果我想将notification变量设置为true(这是我在正文中引用的通知)。

我很确定如果我知道怎么做,我可以弄明白其余的。请原谅我在使用车把方面缺乏经验/知识。我也很擅长使用Meteor。提前谢谢!

PS:我可能完全走错了路,但这就是我问这个问题的原因。

1 个答案:

答案 0 :(得分:1)

很难相信没有人回答过这个问题!

<强> JS:

'click #noticationTestLink' : function(event) {
   event.preventDefault();
   Session.set('notification',true);
}

Template.myTemplate.helpers({
  notification: function(){
    return Session.get('notification');
  }
});

你需要一个模板,Meteor将自动用<body>...</body>包裹它:

 <template name="myTemplate">
   {{#if notification}} 
     {{> notifier}}
   {{else}}
     {{#if currentUser}}
       {{> dashboard}}
     {{else}}
       {{> login}}
     {{/if}}
   {{/if}}
 </template>
相关问题