-
-
Save jeremyjs/edd91510eeb8b27d9853 to your computer and use it in GitHub Desktop.
Reactive publish-counts with computation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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