【问题标题】:Meteor fire an event after Session.setMeteor 在 Session.set 之后触发一个事件
【发布时间】:2015-07-20 09:47:22
【问题描述】:

我想在模板中关注Session.set("quizStarted", true);之后的输入框

Template.quiz.startQuiz = function(){
    return Session.get("quizStarted");
 }

startQuizAnimate = function(){
    Session.set("quizStarted", true);

    $('.answerBox').focus();   //THIS LINE DOES NOT FOCUS
 }

Template.quiz.events({
  'click #startQuiz' : function(){
    startQuizAnimate();
  }
}


<template name="quiz">

 <button id="startQuiz">Start!</button>

  {{#if startQuiz}}

    <form class="answer">
      // I want to focus this input text
      <input type="text" class="answerBox" name="text" placeholder="Type your answer here" />
    </form>
  {{/if}}
  </div>
</template>

{{#if startQuiz}} 内的块包含我想要关注的输入,但我必须在Session.set("quizStarted", true); 完成渲染后调用它。如何使用 Meteor 的最佳实践编写此内容?

【问题讨论】:

    标签: javascript session meteor


    【解决方案1】:

    使用Tracker.afterFlush 注册自定义代码,以便在重新运行当前无效的每个计算后运行,特别是负责重新呈现模板并在{{#if startQuiz}} 变为真后插入表单标记的反应式模板计算。

    function startQuizAnimate(){
      Session.set("quizStarted", true);
      Tracker.afterFlush(function(){
        $('.answerBox').focus();
      });
    }
    

    【讨论】:

    • 你让我很头疼,这是完美的解决方案!
    • Tracker.afterFlush 确实似乎解决了很多人的问题,不是吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多