Meteor 1.0.2.1会话变量没有保持设置

时间:2015-01-08 03:17:17

标签: javascript meteor

在以下代码中,当我点击按钮时,我希望{{#if adding_answers}}会话变量为true。它会触发"click .setup_answers':function",但模板{{> newAnswers}}永远不会显示,因为{{#if adding_answers}}不是真的。我看到它通过console.log设置为true但是它没有返回HTML。 我认为通过将它设置为流星反应会导致它被看到的值?

HTML代码

<template name="newQuestions">
    {{#if adding_answers}} <!-- Session Variable -->
        {{> newAnswers}}
    {{/if}}

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Questions</h3>
                <p></p>
                <form class="form-horizontal">
                    <div class="form-group">
                        <label class="sr-only" for="inputQuestion">Question</label>
                        <input type="textarea" class="form-control" name="inputQuestion"     id="inputQuestion" autofocus="1" placeholder="Enter New Question" rows="5">
                    </div>
                    <button type="button" class="setup_answers btn-primary">Setup Answers</button>
                </form>
            </div>
        </div>
</template>`

JS代码

`Template.newQuestions.events({
  "click .archive": function(event, template){
    return Meteor.call('archiveTodo',this._id,!this.archived);
  },
  'click .todochecked':function(event,template){
    return Meteor.call('completeTodo',this._id,!this.completed);
  },
  'click .setup_answers':function(event,template){
    var question = $('#inputQuestion').val(); // Question must be entered.
    if (question === "") {
      alert("The question can not be blank.");
      Session.set('adding_answers',false); 
    } else {
      Session.set('adding_answers',true); // Causes template newAnswers to be displayed
    }
    return session.get('adding_answers');
  }
});`    

1 个答案:

答案 0 :(得分:0)

首先,您无法从会话变量

等事件处理程序返回任何内容

第二,您需要为adding_answers定义帮助器,如下所示,代码为

Template.newQuestions.helpers({
   adding_answers: function(){
    return Session.get('adding_answers')
  }
})

详细了解助手here