Javascript 如何通过Meteor模板中的ID访问另一个集合?
假设我有一个Posts集合,其数据如下所示:Javascript 如何通过Meteor模板中的ID访问另一个集合?,javascript,mongodb,meteor,handlebars.js,spacebars,Javascript,Mongodb,Meteor,Handlebars.js,Spacebars,假设我有一个Posts集合,其数据如下所示: var post1 = { title: "First post", post: "Post content", comments: [<comment id>, <another comment id>, <etc>] }; Template.post.helpers({ displayComment: function(id) { return Comments.findOne(id)
var post1 = {
title: "First post",
post: "Post content",
comments: [<comment id>, <another comment id>, <etc>]
};
Template.post.helpers({
displayComment: function(id) {
return Comments.findOne(id).fetch().comment;
}
});
并且做:
<template name="post">
<h1>{{title}}</h1>
<p>{{post}}</p>
{{#each comments}}
{{displayComment @index}}
{{/each}}
</template>
{{title}}
{{post}}
{{{#每个评论}
{{displaycoment@index}}
{{/每个}}
但是接下来我必须为每个注释对象的属性创建一个助手,等等
清洁的方法是什么?我不想在post对象上填充comments字段,因为这意味着调用.fetch(),posts将不再是被动的 有几点建议:
<template name="post">
<h1>{{title}}</h1>
<p>{{post}}</p>
{{#each comments}}
{{#with commentDetails}}
{{userName}} //
{{content}} // These are properties of a comment document
{{upvotes}} //
{{/with}}
{{/each}}
</template>
Template.post.helpers({
commentDetails: function() {
return Comments.findOne(this);
}
});
<template name="post">
<h1>{{title}}</h1>
<p>{{post}}</p>
{{#each comments}}
{{#with commentDetails}}
{{userName}} //
{{content}} // These are properties of a comment document
{{upvotes}} //
{{/with}}
{{/each}}
</template>
Template.post.helpers({
commentDetails: function() {
return Comments.findOne(this);
}
});
<template name="post">
<h1>{{title}}</h1>
<p>{{post}}</p>
{{#each commentCursor}}
{{content}}
... // other properties
{{/each}}
</template>
Template.post.helpers({
commentCursor: function() {
return Comments.find({_id: {$in: this.comments}});
}
});