我一直在开发一个使用评论功能的应用程序。这导致必须同时订阅发表评论的集合和评论集合本身。现在看起来像这样:
<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 中处理评论的订阅?
最佳答案
是的,你有两种方法:
Controller 中的逻辑。您可以使用数组订阅多个集合。当您立即显示所有评论时,这将是您的方式。
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)}
}
});
如果您不想在用户请求之前显示评论。您可以按照另一种方式:
您可以将 buttonclick
上的 bookId
设置到 Session 变量中。您可以定义一个 Deps.autorun 函数,该函数使用 Session 变量中提供的 bookId
订阅评论集合。在您的评论模板中,您只需执行正常的收集请求。如果您需要有关此方式的更多提示,请告诉我。
关于javascript - 铁路由器中的多个订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22940628/