我一直在开发一个使用评论功能的应用程序。这导致必须订阅发表评论的集合和评论集合本身。现在看起来像这样:
<template name="bookView">
{{> book}}
{{> comments}}
</template>
this.route('book', {
path: '/book/:_id',
template: 'bookView',
waitOn: function() { return Meteor.subscribe('book');},
action: function () {
if (this.ready()){
this.render();
}
else
this.render('loadingTemplate');
},
data: function() {return Books.findOne(this.params._id);}
});
但现在我想加载属于该书的所有评论。或者我应该在 Template.comments.rendered 中处理评论订阅?
是的,你有两种方法:
控制器中的逻辑。您可以使用数组订阅多个集合。当您立即显示所有评论时,这将是您要走的路。
this.route('book', {
path: '/book/:_id',
template: 'bookView',
/* just subscribe to the book you really need, change your publications */
waitOn: function() {
return [Meteor.subscribe('book', this.params._id),
Meteor.subscribe('comments', this.params._id)];
},
data: function() {
return {
book : Books.findOne(this.params._id),
comments: Comments.find(this.params._id)}
}
});
如果您不想在用户请求之前显示评论。您可以按照另一种方式:
您可以设置bookId
on buttonclick
到会话变量中。您可以定义一个 Deps.autorun 函数,该函数使用以下命令订阅评论集合bookId
在您的会话变量中提供。在您的评论模板中,您只需执行正常的收集请求即可。如果您需要有关此方式的更多提示,请告诉我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)