2

I know how to conditionally publish data in meteor, but how do you conditionally subscribe? I'm thinking of the case where you have an application with separate pages (using meteor-router, for example) and you only need certain subscriptions on certain pages.

Community
  • 1
  • 1
Diogenes
  • 2,697
  • 3
  • 26
  • 27

2 Answers2

2

Use if statements in a Deps.autorun() method. The if statements should test against Session variables, which will make it reactive, triggering Deps.autorun to change the subscriptions when the conditions change. Thus:

Deps.autorun(function() {
    sub.stop(); // Stop the previous subscription, because it's changed.
    if (Session.equals('page', 'foo')) {
        sub = Meteor.subscribe('bar');
    } else if (Session.equals('page', 'lorem')) {
        sub = Meteor.subscribe('ipsum');
    }
    // etc.
});
BenjaminRH
  • 11,974
  • 7
  • 49
  • 76
1

You could also do a multi collection publish/subscription depending on the page. As the page changes the subscription is automatically changed depending on what the page is:

client side js

Deps.autorun(function() {
    Meteor.subscribe("page_subscriptions", Meteor.Router.page());
});

server side js

Meteor.publish("page_subscriptions", function(page) {
    if(page=="home") {
        return [
                Collection1.find({}),
                Collection2.find({}),
                Collection3.find({})
        ];
    }else if(page=="about") {
        return [
                Collection3.find({}),
                Collection4.find({})
        ];
    }
});

So each page has its own set of collections which can have individualized queries, while using the same subscription.

Because Meteor.Router.page() is reactive, as soon as you route to a different page the subscription will be changed.

Tarang
  • 75,157
  • 39
  • 215
  • 276
  • This is interesting, where is ```page``` coming from? And how could you set up and tear down publications on the server dynamically when the page changes? – Diogenes May 23 '13 at 14:45
  • Page comes from the parameter passed from the subscription, from `Meteor.router.page()`. When the subscription variable is changed, the old one is automatically stopped and the new subscription with the new page name is made. Because it is reactive it will all be automatically updated with the new subscription without much more code whenever the page is changed with Meteor Router – Tarang May 23 '13 at 15:37
  • But who is passing ```page``` to that subscription function? – Diogenes May 23 '13 at 19:29
  • Its on the client side at `Meteor.subscribe("page_subscriptions", Meteor.Router.page());` from `Meteor.Router.page()` which is from Meteor Router. So when the current page is changed (from the name of the template in router, the subscription is changed). The subscribe can take a parameter which will be passed on to the corresponding publish function on the server, which is what is called `page` on the server end, so it directly gets `Meteor.Router.page()` when it changes. So this is how the server can 'see' what page the client wants – Tarang May 23 '13 at 19:33
  • From the docs at : http://docs.meteor.com/#meteor_subscribe. Shows how subscribe functions can take parameters that can be passed to the publish function on the server – Tarang May 23 '13 at 19:37
  • Aha, thats good. I'm using your approach with the ```sub.stop()``` from the first answer. – Diogenes May 23 '13 at 19:39
  • You wont have to stop the subscription though, as soon as the page changes the old subscription will be automatically stopped and the new one made instead like in the chat room example in the docs, when the room is changed – Tarang May 23 '13 at 19:44
  • Interesting. An Meteor.publish() doesn't need to be in an autorun() or anything, because the subscription is? – Diogenes May 23 '13 at 19:51
  • The publish function is on the server so that will respond to the whatever changes the subscribe function makes. So as immediately as the page is changed the subscription will request new data and it can respond reactively :-D without being in a autorun block. (I don't think autorun works on the server anyway, but not too sure) – Tarang May 23 '13 at 20:02