Meteor - 如何限制模板级订阅的范围

Meteor - How to limit the scope of template level subscriptions

我的一个合集有两个出版物。一个发布所有数据,另一个仅发布用户特定数据。我想使用首页上的数据来显示一般数据和用户特定数据。 因此主模板包含两个子模板:

主模板:

<template name="index">

    {{> testAll }}

    {{> testUser }}

</template>

模板 'testAll' 和 'testUser' 有自己对相应出版物的订阅,因为数据是特定于模板的。

出版物:

Meteor.publish('dataAll', function() {
    let data = Data.find({});
    if (data) {
        return data;
    }
    return this.ready();
});

Meteor.publish('dataUser', function() {
    let data = Data.find({userId: this.userId});
    if (data) {
        return data;
    }
    return this.ready();
});

订阅:

testAll.js

Template.testAll.onCreated(() => {
    let template = Template.instance();
    template.subscribe('dataAll'); 
});

testUser.js

Template.testUser.onCreated(() => {
    let template = Template.instance();
    template.subscribe('dataUser'); 
});

问题:两个模板订阅的数据都可以访问,但不限于每个模板。

示例(条形不应位于彼此内部,而是分开显示):

问题:如何设置每个模板的范围,使订阅的数据只能在特定模板内访问?

即使我从一个模板中删除订阅,另一个模板仍然可以访问它:

示例(已删除 testUser.js 的订阅):

订阅不会创建单独的数据上下文,它们用于决定将哪些数据放在本地数据库副本 .您应该像对待任何数据库一样对待本地数据库,特别是,您的客户端只有一个全局副本。如果您想限制特定的数据上下文或查询,您应该在查询本身中进行。换句话说,您应该 永远不会 依赖于您对本地数据库中的内容和不存在的知识,因为应用程序的其他部分会影响状态。你应该总是在特定的地方查询你需要的数据。

因此在 testUser 模板中使用 Data.find({userId: this.userId}) 获取 数据并订阅它。