2016-01-23 46 views
1

我試圖動態生成兩組不同的數據表。我的數據庫不是空的,而且退貨也已經過驗證。但是當我檢查呈現的頁面時,相應的html不存在,就好像沒有返回。爲什麼不是流星注入我的模板助手的文本?

模板/ HTML:

<template name="room"> 
<div class="container-fluid"> 
<h1> Sprint Retrospective</h1> 
<hr> 
<div class="input-group"> 
<input type="text" class="form-control thoughts" placeholder="Thoughts..." aria-describedby="basic-addon1"> 
<span class="input-group-addon"> 
     <input id="wentWell" type="checkbox" aria-label="..."> Went Well 
     </span> 
<span class="input-group-addon"> 
     <input id="wentWrong" type="checkbox" aria-label="..."> Went Wrong 
     </span> 
<span class="input-group-btn"> 
     <button class="btn btn-default" type="button">Submit!</button> 
     </span> 
</div> 
<hr> 
{{#if haveCards}} 
    <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-xs-6 col-sm-6"> 
       <div class="row">Went Well</div> 
       {{#each wentWell}} 
        {{>card}} 
       {{/each}} 
      </div> 
      <div class="col-xs-6 col-sm-6"> 
       <div class="row">Went Wrong</div> 
       {{#each wentWrong}} 
        {{>card}} 
       {{/each}} 
      </div> 
     </div> 
    </div> 
{{/if}} 
</div> 
</template> 

的Javascript:

"use strict"; 
/** 
* 
**/ 
var Cards = new Mongo.Collection('cards'); 
var allCards; 
var wentWellCards; 
var wentWrongCards; 
if(Meteor.isClient){ 
    Tracker.autorun(function(){ 
     allCards = Cards.find({},{sort:{createdAt:-1}}); 
     wentWellCards = Cards.find({category:"Went Well"},{sort:{createdAt:-1}}); 
     wentWrongCards = Cards.find({category:"Went Wrong"},{sort:{createdAt:-1}}); 
    }); 
    Template.room.helpers({ 
     haveCards: function(){ 
      if(allCards != null && allCards != undefined && allCards.length > 0) 
       return true; 
      return false; 
     }, 
     wentWell: function(){ 
      return this.wentWellCards; 
     }, 
     wentWrong: function(){ 
      return this.wentWrongCards; 
     } 
    }); 
} 

回答

2

傑里米回答它實際上是在點多,但..

讓我們嘗試修復這些代碼一點點。

讓我們更改wentWellwentWrong傭工看起來更像這樣乾淨。

wentWell: function(){ 
     return Cards.find({category:"Went Well"},{sort:{createdAt:-1}}); 
    }, 
    wentWrong: function(){ 
     return Cards.find({category:"Went Wrong"},{sort:{createdAt:-1}}); 
    } 

還在爲haveCards助手,你可以這樣做

0

你的助手應該返回wentWellCards而不是this.wentWellCards

0

你的助手都沒有反應,所以,當數據被加載(發生在頁面呈現後)輔助程序不會重新運行。

只需直接在助手中調用被動方法(minimongo查詢)即可。這將讓他們重新運行一次的數據是可用的

此外,當您檢查計數,你需要獲取集合

Cards = new Mongo.Collection('cards'); 
if(Meteor.isServer){ 
    Meteor.publish('cards', function() { 
     return Cards.find({},{sort:{createdAt:-1}}); 
    }); 
} 

if(Meteor.isClient){ 
    Template.room.onCreated(function(){ 
     this.subscribe('cards'); 
    }); 
    Template.room.helpers({ 
     haveCards: function(){ 
      var allCards = Cards.find({},{sort:{createdAt:-1}}).fetch(); 
      return (allCards != null && allCards != undefined && allCards.length > 0); 
     }, 
     wentWell: function(){ 
      return wentWellCards = Cards.find({category:"Went Well"},{sort:{createdAt:-1}}); 
     }, 
     wentWrong: function(){ 
      return wentWrongCards = Cards.find({category:"Went Wrong"},{sort:{createdAt:-1}}); 
     } 
    }); 
} 

你會需要從服務器發佈的收集和訂閱從模板(除非您使用自動發佈)

相關問題