使用模板助手在Meteor模板中激活文本

时间:2013-06-22 12:33:07

标签: 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 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>

当我第一次调用该功能时,它会正确显示我需要的内容。之后,每当我再次调用该函数时,无论我做什么,页面上的文本都保持不变。变量currentdesccurrentchoice1currentchoice2会按预期相应更改,但模板不会更新。

2 个答案:

答案 0 :(得分:0)

我在Meteor上很新,但我认为你需要设置一个模板助手。

Template.below.helpers ({
  choice1: function () {
    return currentchoice1;
   }
}),

这使得choice1被动反应。

鲍勃

答案 1 :(得分:0)

使用Session使变量成为被动:

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的文档中使用Session的示例:http://docs.meteor.com/#session

相关问题