铁路由器中的多个订阅 [英] Multiple subscriptions in iron router

查看:114
本文介绍了铁路由器中的多个订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working on an application using a comment function. That results in having to subscribe to both a collection which the comments are made on and the comments collection itself. Now it looks like this:


< template name =bookView>
{{>预订}}
{{>评论}}
< / 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中处理评论的订阅吗?

But now I would like to load all comments belonging to that book also. Or should I handle the subscription of comments in Template.comments.rendered?

推荐答案

是的,你有两种方法:

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

Logic in Controller. You can subscribe with an array to multiple collections. This would be the way you go when you show all the comments instantly.

    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)}
      }
    });

如果您不想在用户请求之前显示评论。您可以按照其他方式:

If you dont want to show comments until they are requested by the user. You can follow another way:

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

You can set the bookId on buttonclick into a Session variable. Than you can define a Deps.autorun function which subscribes to the comments collection with the bookId provided in your Session variable. In your comments template you just have to do the normal collection request. If you need more hints about this way let me know.

这篇关于铁路由器中的多个订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆