Javascript 使用iron:router Meteor中的data:function从两个集合返回数据
不使用publish、waitOn或其他高级技术进行学习。不知怎么的,我没法让它工作Javascript 使用iron:router Meteor中的data:function从两个集合返回数据,javascript,meteor,iron-router,Javascript,Meteor,Iron Router,不使用publish、waitOn或其他高级技术进行学习。不知怎么的,我没法让它工作 Router.route('/browse/:_id', function(){ this.render('navigation', {to: 'navbar'}); this.render('header', {to: 'header'}); this.render('website', { to: 'main
Router.route('/browse/:_id', function(){
this.render('navigation', {to: 'navbar'});
this.render('header', {to: 'header'});
this.render('website', {
to: 'main',
data: function(){
return {
websites: Websites.findOne({_id: this.params._id}),
comments: Comments.find({siteId: this.params._id})
}
}
});
});
此代码适用于“评论”,而“网站”不会呈现。“网站”下的字段仅在我按如下方式返回时才会以HTML呈现:
data: function(){ /*This works for websites but if used on comments it won't*/
return Websites.findOne({_id: this.params._id})
}
而对于“注释”,它仅在我以对象表示法或键值对给出时有效:
data: function(){ /*This works for comments but if used on websites it wont*/
return {comments: Comments.find({siteId: this.params._id})}
}
我的问题是我需要从同一模板中的两个集合中获取数据。任何建议都是非常受欢迎的。谢谢
编辑也包括我的HTML模板:“网站”模板
<template name="website">
<div class="container">
/*solved by including #with websites here*/
<div class="website">
<div class="website-header js-url">
<h4>{{title}}</h4>
</div>
<div class="website-body">
<tr><td>URL:</td> <td>{{url}}</td></tr>
<tr><td>Description</td><td>{{description}}</td></tr>
<div class="votes">
<div class="upvote_row">
<tr>
<td>Up Votes:</td>
<td>{{upVote.length}}<a href="#" class="btn btn-default js-upvote"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span></a></td>
</tr>
</div>
<div class="downvote_row">
<tr>
<td>Down Votes:</td>
<td>{{downVote.length}}<a href="#" class="btn btn-default js-downvote"><span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span></a></td>
</tr>
</div>
</div>
<tr><td>Posted By:</td><td><a href class="">{{createdBy}}</a> on {{createdOn.toDateString}} at {{createdOn.toLocaleTimeString}} </td></tr>
</div>
</div>
/* /with */
<div class="comments">
<h5>Comment Section</h5>
{{#if currentUser}}
<a class="btn btn-default js-toggle-comment-form" href>
<span class="glyphicon glyphicon-comment" aria-hidden="true"></span>
</a>
{{/if}}
<div id="comment_form" class="hidden_div">
<form class="js-save-comment-form">
<div class="form-group">
<label for="user_comment">Add Comment</label>
<input type="textfield" class="form-control" id="user_comment" placeholder="Your views on this website" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
<button class="btn btn-default js-cancel-submit">Cancel</button>
</form>
</div>
<ol>
{{#each comments}}
{{>comment_item}}
{{/each}}
</ol>
</div>
</div>
</template>
/*通过在此处添加网站解决*/
{{title}}
URL:{{URL}}
描述{{Description}}
赞成票:
{{upVote.length}
反对票:
{{downVote.length}
发布人:在{{createdOn.toDateString}上的{{createdOn.toLocaleTimeString}}
/*/与*/
评论部分
{{{#如果当前用户}}
{{/if}
添加注释
提交
取消
{{{#每个评论}
{{>注释项目}
{{/每个}}
然后我的评论模板,工作完美
<template name="comment_item">
<li class="card-panel teal darken-1 comments">
<blockquote>
<p class='card-content'>
{{userComment}}
</p>
<footer>
<a href="{{url}}">{{postedBy}}</a> on {{postedOn.toDateString}} at {{postedOn.toLocaleTimeString}}
<a href="#" class="btn btn-default js-upvote">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
<span class="badge badge-notify">{{upVote.length}}</span>
</a>
<a href="#" class="btn btn-default js-downvote">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
<span class="badge badge-notify">{{downVote.length}}</span>
</a>
</footer>
</blockquote>
</li>
</template>
{{userComment}}
关于{{postedOn.toDateString}}at{{postedOn.toLocaleTimeString}}
您还应该显示HTML模板代码。我怀疑您是直接引用websites对象的键,而不是使用{{websites.keyname}
。基于您的数据功能,您的数据上下文将是{websites:a object,comments:a cursor}
如果您尝试从您的网站访问{{keyname}
,您将一无所获,因为您的数据上下文没有名为keyname的顶级键。有两件事:1。)我会尝试使用带有导航的模板,页眉、页脚等都包含在模板中,然后呈现单个模板。2.)我不知道它为什么会这样,但这可能与findOne()将返回一个对象,而find()将返回一个光标有关。试着改用find().fetch()也许吧?嗨,我们如何设置keyname
?你是对的,我是如何从我的模板中引用网站上的键的。昨天我已经用上面相同的代码解决了这个问题(第一个是w/two-on),在我开始引用网站
keyname
中的键之前,我加入了{{with-websites}}之后,它就是网站
文档中的顶级键。如果有一个名为foo
的顶级键,那么您可以使用{{websites.foo}