This.stop() - subscribe to new publication on same template

Hi, I have a single template that shows a subset of books from different categories (the collection is books). Users can refresh to see a new subset of books from the particular category they’ve chosen.

With the default category, refreshes work fine. I simply call sub_handle.stop() and local cache clears on the client & new items are loaded from the same, default category.

But I’m not sure how to change publications. Say, from ‘default’ to ‘fiction’ publications.

Simply subscribing via Meteor.subscribe('fiction') adds to what’s already in Minimongo, not replacing what was already there. I also tried assigning the new subscription to the original variable sub_handle without success. EDIT: this also happens when I clear the local collection via Books._collection.remove({});.

Is there a way to call this.stop() and specify a new publication? Specifically, how can I subscribe to a new publication without changing templates and clear the local cache at the same time?

I also tried arunoda’s subs-manager but didn’t have any success.

Stopping should not be necessary in this case… If you use a reactive var inside a templateInstance.autorun the old subscription will be stopped and the new one subscribed to. For example

    var self = this;
        //using session here for simplicity, reactive var will work as well
        var category = Session.get("category");

        self.subscribe("bookCategory", category);

this will stop subscriptions whenever the autorun computation is invalidated and thus also clean up after the template is destroyed.

As a side note, you should probably also be filtering your query for the category as well so that you aren’t relying solely on publications to remove the old data from the view.

    books: function() {
        var category = Session.get("category");
        return Books._collection.find({category:category});