Javascript 使用模板帮助器使Meteor模板中的文本具有反应性
我有一个功能:Javascript 使用模板帮助器使Meteor模板中的文本具有反应性,javascript,meteor,spacebars,Javascript,Meteor,Spacebars,我有一个功能: function Print(string, number) { if (number == 1) { // For descriptions currentdesc = string; } else if (number == 2) { // For the first choice currentchoice1 = string; } else
function Print(string, number) {
if (number == 1) { // For descriptions
currentdesc = string;
}
else if (number == 2) { // For the first choice
currentchoice1 = string;
}
else if (number == 3) { // For the second choice
currentchoice2 = string;
}
}
以及一些使用这些值在页面上显示它们的模板:
if (Meteor.isClient) {
Template.main.desc = currentdesc;
Template.below.choice1 = currentchoice1;
Template.below.choice2 = currentchoice2;
}
其中的HTML部分是:
<template name="main">
<h2>Current:</h2>
{{desc}}
</template>
<template name="below">
<h3>Choose:</h3>
{{choice1}}
<br/>
{{choice2}}
<br/>
<div>
<input id="number" type="text" size="40">
<input type="button" class="choose" Value="choice">
</div>
</template>
当前:
{{desc}}
选择:
{{choice1}}
{{choice2}}
当我第一次调用函数时,它会正确显示我需要的内容。之后,每当我再次调用该函数时,无论我做什么,页面上的文本都保持不变。变量
currentdesc
、currentchoice1
和currentchoice2
会按预期相应地更改,但模板不会更新。我对Meteor很陌生,但我认为您需要设置模板助手
Template.below.helpers ({
choice1: function () {
return currentchoice1;
}
}),
这使得choice1变得被动
Bob使用
会话
使变量反应:
function Print(string, number) {
if (number == 1) { // For descriptions
Session.set('currentdesc', string);
}
else if (number == 2) { // For the first choice
Session.set('currentchoice1', string);
}
else if (number == 3) { // For the second choice
Session.set('currentchoice2', string);
}
}
以及您的模板:
if (Meteor.isClient) {
Template.main.desc = function() {
return Session.get('currentdesc');
}
Template.below.choice1 = function() {
return Session.get('currentchoice1');
}
Template.below.choice2 = function() {
return Session.get('currentchoice2');
}
}
关于如何在Meteor的文档中使用会话
,还有几个例子: