最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple subscriptions in iron router - Stack Overflow

programmeradmin1浏览0评论

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

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

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

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?

Share Improve this question asked Apr 8, 2014 at 14:50 Eric LundmarkEric Lundmark 2293 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 27

Yeah you have two ways:

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:

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.

Your waitOn function can wait for multiple subscriptions by returning an array of the subscription handles.

发布评论

评论列表(0)

  1. 暂无评论