Skip to content

Instantly share code, notes, and snippets.

@jeremyjs
Forked from boxofrox/publish-counts-issue-35.js
Last active August 29, 2015 14:27
Show Gist options
  • Save jeremyjs/edd91510eeb8b27d9853 to your computer and use it in GitHub Desktop.
Save jeremyjs/edd91510eeb8b27d9853 to your computer and use it in GitHub Desktop.
Reactive publish-counts with computation example
// Don't leak observes in Meteor.publish callback.
// Define only one instance for server of buyer and distributor ids for reactive counts.
var BuyerQueue = new Meteor.Collection(null);
var DistributorQueue = new Meteor.Collection(null);
function isBuyer (doc) {
return _.include(doc.roles, 'buyer');
}
function isDistributor (doc) {
return _.include(doc.roles, 'distributor');
}
// observe changes to users query.
Meteor.users.find({
$or: [
{
'roles': 'distributor',
'profile.status': 'PENDING',
'profile.agreedToTOC': true,
},
{
'roles': 'buyer',
'profile.status': 'PENDING',
},
]
}).observe({
added: function (doc) {
if (isBuyer(doc))
BuyerQueue.insert({ _id: doc._id });
else if (isDistributor(doc))
DistributorQueue.insert({ _id: doc._id });
},
changed: function (oldDoc, newDoc) {
if (! isBuyer(newDoc))
BuyerQueue.remove(newDoc._id);
if (! isDistributor(newDoc))
DistributorQueue.remove(newDoc._id);
},
removed: function (doc) {
BuyerQueue.remove(doc._id);
DistributorQueue.remove(doc._id);
}
});
Meteor.publish('pendingReview', function () {
if(Roles.userIsInRole(this.userId, 'compliance')) {
Counts.publish(this, 'remainingBuyers', BuyerQueue.find({}));
Counts.publish(this, 'remainingDistributors', DistributorQueue.find({}));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment