javascript - 铁路由器中的多个订阅

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

最佳答案

是的,你有两种方法:

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/

相关文章:

javascript - 正则表达式阻止连续 4 次以上包含相同数字的电话号码?

meteor - 在 Meteor 服务器中使用 Handlebars util 方法

javascript - Meteor - 在路由中的 React 模板中使用 Blaze 模板

javascript - 当订阅已经限制了数据集时,是否有必要将限制传递给数据?

meteor - Iron 路由器子布局 - 获取产量数据上下文

没有 'www' 前缀, meteor 链接不起作用

javascript - 我需要一个计时器来暂时禁用鼠标悬停事件

javascript - 检查列表中的元素是否具有来自变量的 SRC,然后运行代码 - jQuery/Javascript

javascript - 检测 javascript 或 jQuery 中的上一个 url

javascript - meteor 鲁巴萨 :Sortable Nested Field