Reputation: 709
I have two publications for one Collection. One publishes all data, the other one only user-specific data. I'd like to use the data on the front page to display general and user-specific data. Therefore the Main template contains two sub-templates:
Main template:
<template name="index">
{{> testAll }}
{{> testUser }}
</template>
The Templates 'testAll' and 'testUser' have their own subscriptions to the corresponding publications since the data is template specific.
Publications:
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();
});
Subscriptions:
testAll.js
Template.testAll.onCreated(() => {
let template = Template.instance();
template.subscribe('dataAll');
});
testUser.js
Template.testUser.onCreated(() => {
let template = Template.instance();
template.subscribe('dataUser');
});
Problem: The data both templates subscribe to is accessible for both and not scoped to each template.
Example (The bars should not sit inside each other but displayed separately):
Question: How can i set the scope for each template so the data subscribed to is only accessible inside the specific template?
Even when i remove a subscription from one template the other one has still access to it:
Example (subscription from testUser.js removed):
Upvotes: 0
Views: 196
Reputation: 19544
Subscriptions don't create a separate data context, they're used to decide which data is placed in the local database copy. You should treat the local database just as you treat any database, in particular, there is only one copy global to your client. If you want to limit a specific data context or query, you should do it in the query itself. In other words, you should never rely on your knowledge of what is in the local database and what is not, as other parts of the application can influence the state. You should always query the data you need in the particular place.
So in the testUser
template use Data.find({userId: this.userId})
to fetch the data as well as to subscribe it.
Upvotes: 1