Javascript 为什么不是';t meteor从我的模板助手中注入文本?
我试图动态生成一个包含两组不同数据的表。我的数据库不是空的,返回的数据也经过了验证。但是当我检查呈现的页面时,相应的html不在那里,好像什么也没有返回 模板/html:Javascript 为什么不是';t meteor从我的模板助手中注入文本?,javascript,html,mongodb,meteor,Javascript,Html,Mongodb,Meteor,我试图动态生成一个包含两组不同数据的表。我的数据库不是空的,返回的数据也经过了验证。但是当我检查呈现的页面时,相应的html不在那里,好像什么也没有返回 模板/html: <template name="room"> <div class="container-fluid"> <h1> Sprint Retrospective</h1> <hr> <div class="input-group"> <input typ
<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>
杰里米回答说得更准确,但是 让我们试着稍微修改一下代码 让我们更改
wentWell
和wentWrong
帮助程序,使其看起来更干净
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()
}
您的助手应该返回
wentWellCards
,而不是this.wentWellCards
,等等。您的助手不是被动的,因此,当加载数据时(在呈现页面后发生),助手不会重新运行
简单地说,直接在helper中调用被动方法(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}});
}
});
}
您需要从服务器发布集合并从模板订阅(除非您使用的是autopublish)
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}});
}
});
}