铁路由器中的多个订阅

Eri*_*ark 10 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);}
});
Run Code Online (Sandbox Code Playgroud)

但是现在我想加载属于那本书的所有评论.或者我应该在Template.comments.rendered中处理评论的订阅?

cha*_*hne 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)}
      }
    });
Run Code Online (Sandbox Code Playgroud)

如果您不想在用户请求之前显示评论.你可以按照另一种方式:

您可以将bookIdon 设置buttonclick为Session变量.您可以定义一个Deps.autorun函数,该函数使用bookIdSession变量中提供的注释集合进行订阅.在您的评论模板中,您只需要执行正常的收集请求.如果你需要更多关于这种方式的提示,请告诉我.


Pep*_*L-G 5

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