【问题标题】:Meteor 1.0.2.1 session variable not staying setMeteor 1.0.2.1 会话变量未设置
【发布时间】:2015-01-08 13:03:07
【问题描述】:

在下面的代码中,当我单击按钮时,我期望 {{#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');
  }
});`    

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    首先,您不能从事件处理程序中返回任何内容,例如 Session 变量

    其次,您需要为adding_answers 定义帮助器,如下所示,以供炒锅

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

    阅读更多关于助手here

    【讨论】:

    • @pld32 好的,这看起来像个大项目,你能指出它没有被使用的地方吗?
    • @pld32 我看到它正在被使用,查看client\views,然后查看js 目录中的任何js文件
    • OMG- 现在情况看起来不错。你能为我澄清一下会话变量吗?我看到我在控制台中收到“会话未定义”。会话变量是否必须先定义默认值或其他内容?
    • 当然,如果您没有定义会话变量并尝试通过Session.get('variableName') 进行相同的访问,那么您将获得undefined 作为值。你可以设置它的默认值Session.setDefaullt
    • 并且您得到“会话未定义”,因为您在小情况下使用带有 s 的“会话”SessionS 大写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多