简体   繁体   English

在Session.set之后,Meteor会触发一个事件

[英]Meteor fire an event after Session.set

I want to focus an input box in my template after Session.set("quizStarted", true); 我想在Session.set("quizStarted", true);之后将输入框聚焦在我的模板中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>

The block inside {{#if startQuiz}} contains the input I want to focus on, but I have to call it after Session.set("quizStarted", true); {{#if startQuiz}}中的块包含我想要关注的输入,但我必须在Session.set("quizStarted", true);之后调用它Session.set("quizStarted", true); finishes rendering. 完成渲染。 How can I write this with Meteor's best practices? 我怎样才能用Meteor的最佳实践来写这个?

Use Tracker.afterFlush to register custom code to be run once every computations currently invalidated have been rerun, in particular the reactive template computation responsible for rerendering the template and insert the form markup after {{#if startQuiz}} becomes truthy. 使用Tracker.afterFlush注册要在当前无效的每个计算重新运行后运行的自定义代码,特别是负责重新呈现模板并在{{#if startQuiz}}变得真实之后插入表单标记的反应模板计算。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM