为什么流星不从我的模板助手中注入文本?

时间:2016-01-23 23:05:19

标签: javascript html mongodb meteor

我试图动态生成一个包含两组不同数据的表。我的数据库不为空,返回也已经过验证。但是,当我检查渲染的页面时,相应的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;
        }   
    });
}

3 个答案:

答案 0 :(得分:2)

Jeremy实际上回答的更多,但是......

让我们尝试修改一下这段代码。

让我们更改wentWellwentWrong助手,使其看起来更干净。

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

对于haveCards助手,您可以执行类似

的操作
haveCards: function(){
 return Cards.find().count() >= 1 //for example or return just findOne() 
}

答案 1 :(得分:0)

您的助手应该返回wentWellCards而不是this.wentWellCards

答案 2 :(得分: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}});
        }   
    });
}

您需要从服务器发布集合并从模板订阅(除非您使用自动发布)