MeteorJS出版物超出范围

时间:2016-11-25 11:34:57

标签: javascript meteor meteor-publications

我有以下模板:

<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>

我已将出版物附加到reportsSummary模板,但它似乎也扩展到reportsPatients模板。我不确定为什么因为我按照正确的方法来定义pubs / subs(我认为......)。

我知道它正在扩展到reportsPatients,因为如果我从Appointments.find()助手返回reportsPatients而没有订阅出版物,我会得到{{1}中的数据}}

这是我的出版物:

reportsSummary

这是我的订阅:

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

我所拥有的并不是打破任何功能本身。当应用程序拥有必须筛选的大量数据时,我只是担心效率。我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

你没有遗漏任何东西,这是Meteor中的正常行为。从服务器发布的数据没有范围。一旦数据发布到客户端,所有客户端都可以访问它们,即使在浏览器控制台中运行的代码也可以这样做。

只有在用于订阅它们的订阅关闭时,才会在客户端中清除这些发布的数据。与您的示例中一样,因为您在this.subscribe模板中使用了reportsSummary,因此当reportsSummary被销毁时(onDestroyed事件被触发时),此订阅将被关闭。

Meteor中的最佳做法是始终在collection.find中查询以获取文档。通过这种方式,您的操作可以明确指出您希望获得的内容,并防止返回不需要的文档。