2015-07-20 88 views
1

後一個事件我想集中一個輸入框在我的模板後Session.set("quizStarted", true);流星火Session.set

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的最佳實踐來寫這篇文章?

回答

3

使用Tracker.afterFlush來註冊自定義代碼以在當前無效的每個計算被重新運行時運行,特別是負責重新渲染模板並在{{#if startQuiz}}變成真值之後插入表格標記的反應性模板計算。

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

你救了我一個很頭疼,這是完美的解決方案! – goda

+0

'Tracker.afterFlush'肯定似乎解決了很多人的問題,不是嗎? :) – CaptSaltyJack