2017-04-20 14 views
0

私は、コレクションの数に基づいて推定待ち時間を表示するアプリケーションを作成しています。私が抱えている問題は、ページが読み込まれたり、waitTimeの表示がリフレッシュされたときです。最初に0が表示され、約1秒後にカウントに基づいて実際のwaitTimeが表示されます。私はこれが、変数が初期カウントを0にしてから実際のカウントを取得してwaitTimeを表示するように、コレクションからカウントを取得するのが遅れていることを前提としていますか?流星での変数の表示を遅らせるには?

ロードまたはリフレッシュ時に正確な待機時間のみを表示する方法はありますか?

JS:

Template.home.helpers({ 
waitTime: function() { 
    var totalCount = Students.find().count(); 
    var hour = totalCount/4; 

    if(totalCount < 4){ 
     return 15*totalCount + " minutes" 
    }else if(totalCount >= 4 && totalCount%4 == 0){ 
     return hour + " hour(s)"; 
    }else if(totalCount >= 4 && totalCount%4 == 1){ 
     hour = hour - .25; 
     return hour + " hour(s)" + " 15 minutes"; 
    }else if(totalCount >= 4 && totalCount%4 == 2){ 
     hour = hour - .5; 
     return hour + " hour(s)" + " 30 minutes"; 
    }else if(totalCount >= 4 && totalCount%4 == 3){ 
     hour = hour - .75; 
     return hour + " hour(s)" + " 45 minutes"; 
    } 
    } 
}); 

HTML:

<template name= "home"> 
<body> 
    <h2 id="insert">Approximate Wait Time: {{waitTime}}</h2> 
    <div class="col-lg-6 col-lg-offset-3"> 
     <!-- Quick form from autoform package creates sign in form and populates collection with data--> 
     {{>quickForm id="studentForm" collection="Students" type="insert" template="bootstrap3-horizontal" label-class="col-sm-3" input-col-class="col-sm-9"}} 
    </div> 
</body> 
</template> 

答えて

1

のような結果を返す

何かあなたのデータが準備されるまでビュー(またはビューの少なくとも関連する部分)を表示します。 2つの主な方法は、サブスクリプションが準備されるのを待つか、必要なデータ値が得られるまで待つことです。

私が知ることから0が可能な値であるため、後者は困難です。だから私は前者を提案するだろう。あなたのサブスクリプションを想定し

がテンプレートに結びついている、あなたは、このような準備ができて、サブスクリプションのを待つことができます。

<template name= "home"> 
<body> 
    {{#if Template.subscriptionsReady}} 
    <h2 id="insert">Approximate Wait Time: {{waitTime}}</h2> 
    <div class="col-lg-6 col-lg-offset-3"> 
     <!-- Quick form from autoform package creates sign in form and populates collection with data--> 
     {{>quickForm id="studentForm" collection="Students" type="insert" template="bootstrap3-horizontal" label-class="col-sm-3" input-col-class="col-sm-9"}} 
    </div> 
    {{else}} 
     Loading... 
    {{/if}} 
</body> 
</template> 
0
var totalCount = Students.find().count(); 

ページがロードされる最初の時間は、流星の反応性はまだ確立していない場合には、このようにカウントは、常に0でありますディスプレイが0になるようにします。サブスクリプションがまだ完了しているかどうかを確認してから、ページを表示する必要があります。

Template.home.created = function() { 
    this.loading = new ReactiveVar(true) 
    let subHandler = Meteor.subscribe('your-publication-name') 
    this.loading.set(!subHandler.ready()) 
} 
0テンプレートのヘルパーで

そして、loadingが真であるかどうかを確認、積載テキストやSTHを返し、それ以外の最も簡単な方法は、あなたをレンダリングしないことです。この

waitTime: function() { 
    if (Template.instance().loading.get()) return "Loading"; 

    // the rest of the code 
} 
関連する問題