javascriptmeteormeteor-publications

MeteorJS publication extending beyond scope


I have the following templates:

<template name="reportsContent">
    <ul class="tabs">
        <li class="tabs-content" data-content="summary">
            <div class="tabs-content-wrapper">
                {{> reportsSummary }}
            </div>
        </li>
        <li class="tabs-content" data-content="patients">
            <div class="tabs-content-wrapper">
                {{> reportsPatients }}
            </div>
        </li>
    </ul>
</template>

<template name="reportsSumary">
    ....
</template>

<template name="reportsPatients">
    ....
</template>

I've attached a publication to the reportsSummary template, yet it seems to be extending to the reportsPatients template too. I'm not sure why since I've followed the correct method to define pubs/subs (I think...).

I know that it's extending to reportsPatients because I if I return Appointments.find() from a reportsPatients helper without subscribing to a publication, I'm getting the data that's also in reportsSummary

Here's my publication:

Meteor.publish('appointments.day.patients', function () {

    var thisMonth = new RegExp(moment().format('MMM YYYY'));

    return Appointments.find({
        date_created: { $regex: thisMonth }
    }, { fields: { date_created: 1 } });
});

And here's my subscription:

Template.reportsSummary.onCreated(function () {
    this.subscribe('appointments.day.patients');
});

It's not that what I have is breaking any functionalities per se. I'm just worried about efficiency when the app has a whole lot of data that it has to sift through. Am I missing something here?


Solution

  • You are not missing anything, that is the normal behaviour in Meteor. Data published from server has no scope. Once data is published to client, all client could access them, even code run in browser console could do that.

    These published data will only be cleared in client when the subscription used to subscribe for them is closed. Like in your example, because you use this.subscribe inside reportsSummary template so this subscription will be closed when reportsSummary is destroyed (when the onDestroyed event is fired).

    It is a best practice in Meteor to always put a query in collection.find to get documents. This way your operation is explicit about what you expect to get, and prevent unwanted documents to be returned.