How subscribing to the same publication more than once works?


#1

Imagine an the page of a Meteor app. that has a left div and a right div on the page, nothing else. The left div contains a list of ALL the items in a Vegetables collection in Mongo, the righ div contains a list of vegetables that are ONLY green.

In terms of setting up the data, the collection is defined on both client/server:
VegetablesColl = new Mongo.Collection("vegetables");

On the server, a publication is set-up:
Meteor.publish('VegetablesPub', function(color) { return VegetablesColl.find({vegetableColor: color}); });

The left div (which is a React component) subscribes to this publication:
... Meteor.subscribe('VegetablesPub'); VegetableData = VegetablesColl.find({}).fetch(); ...

The right div (which is also a React component) also subscribes to this publication:
... Meteor.subscribe('VegetablesPub', 'green'); VegetableData = VegetablesColl.find({}).fetch(); ...

Will this mean that both divs show only green vegetables, both divs show ALL vegetables, or that the left shows ALL and the right shows ONLY the green ones?
Reason I ask is that the logic seems to suggest that what will be in the VegetablesColl (in MiniMongo) will be whatever the publication was last asked to publish, in this case just green vegetables. So I would expect to see green vegetables in both divs (?)

If this is the case, what would I change in order to have the divs showing correctly?


#2

Without having tried it, it seems like it would work.

However, since one div is showing a subset of the other, you can publish the entire Collection and filter it client-side:

Meteor.publish('VegetablesPub', function() { return VegetablesColl.find({}); });

In your components:

Meteor.subscribe('VegetablesPub');

leftDiv = VegetablesColl.find({}).fetch();

rightDiv = VegetablesColl.find({vegetableColor: green}).fetch();