请参阅以下代码:
Template.EditProfile.helpers({
'welcomeMessage': function() {
if (Session.get("welcomeMessage")) {
var welcomeMessage = Session.get("welcomeMessage");
delete Session.keys['welcomeMessage']
return welcomeMessage;
} else {
return false;
}
}
})
在html中,
{{#if welcomeMessage}}
<div class="change-explain red-msg">{{ welcomeMessage }}</div>
{{else}}
<div class="change-explain">People will see your username on your comments.</div>
{{/if}}
这总是什么都没有。如果我在外面if语句,它工作正常。我在这里想到的反应式风格是什么?
答案 0 :(得分:2)
在Meteor中,Session变量本身是被动的,这意味着当你改变&#34; welcomeMessage&#34;的值时会话变量,你的&#34; welcomeMessage&#34;帮助程序将再次运行,不显示任何内容。要对其进行测试,请注释delete Session.keys['welcomeMessage']
并在应用运行时,通过键入Session.set('welcomeMessage', "test")
然后Session.set('welcomeMessage')
如果你想根据该会话变量设置类,你可以添加一个帮助器,如:
'getClass': function() {
var className = "";
if (Session.get("welcomeMessage"))
className = 'red-msg';
return className;
}
和消息助手如:
'getMessage': function() {
var messsage = "People will see your username on your comments.";
if (Session.get("welcomeMessage"))
messsage = Session.get("welcomeMessage");
return messsage;
}
然后你的html看起来像:
<div class="change-explain {{getClass}}">{{getMessage}}</div>