铁路由器中的多个订阅

时间:2014-04-08 14:50:56

标签: javascript meteor iron-router

我一直在使用评论功能处理应用程序。这导致必须订阅发表评论的集合和评论集合本身。现在它看起来像这样:

  

<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中处理评论的订阅吗?

2 个答案:

答案 0 :(得分:27)

是的,你有两种方式:

控制器中的逻辑。您可以使用数组订阅多个集合。当你立即显示所有评论时,这就是你的方式。

    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上的buttonclick设置为Session变量。您可以定义一个Deps.autorun函数,该函数使用Session变量中提供的bookId订阅评论集合。在您的评论模板中,您只需要执行正常的收集请求。如果你需要更多关于这种方式的提示,请告诉我。

答案 1 :(得分:5)

您的waitOn函数可以通过返回订阅句柄数组来等待多个订阅。

相关问题