Search code examples
javascriptmeteorsubscriptionflow-router

How do I use a subscription in flow-router?


// server/publications.js
Meteor.publish('crewMessages', function(crewId) { 
  return CrewMessages.find({crewId: crewId}, {sort: {submitted: -1}, limit: 100});
});

// lib/router.js
FlowRouter.route('/crew/:crewSlug', {
    subscriptions: function(params) {
        console.log("Subscribed to this crew's chat messages:", params.crewSlug);
        this.register('crewMessages', Meteor.subscribe('crewMessages', params.crewSlug));
    },
    action: function(params) {
      FlowLayout.render("layout", { content: 'crew' });
    }
});

And inside my crew.html template:

<div class="container">
  {{> crewChat}}
</div>

And my crewChat.html/js:

Template.crewChat.helpers({
  messages: function() {
    return CrewMessages.find({}, {sort: {submitted: -1}}); 
  }
});

<div class="ui feed">
  {{#each messages}}
    {{> crewChatMessage}}
  {{/each}}
</div>

In my crewChat.js file, how can I use the subscription I set in Flow-router?


Solution

  • I create a functional example for you at MeteorPad

    http://meteorpad.com/pad/Ba5DTe94NjFi3ZTPA/Playground_Flow-Router_Chat

    Most important to your question is, that you just can use the Collection inside your template with Collection.find() because you are only subscribed to the crews messages via route subscription.

    Each message can be accessed as normal document see template showChatMessage

    Hope that makes it clear for you

    Cheers Tom

    PS: You can also change the url inside the meteorpad to also switch to chats for team2 and team3