Created
September 12, 2012 03:01
-
-
Save lwille/3703983 to your computer and use it in GitHub Desktop.
publish count
This file contains hidden or 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
shotAccessControl = (userId, selector, customer)-> | |
if current_user = Meteor.users.findOne userId | |
if customer and (current_user.role is 'admin' or (current_user.customers and customer in current_user.customers)) | |
customer = Customers.findOne(customer) | |
not customer.deleted if customer | |
shotSelector = (selector, customer)-> | |
$and: [ | |
$and:[ | |
(completed: true), | |
$or: [ | |
(deleted: null) | |
(deleted: false) | |
(deleted: ($exists: false)) | |
] | |
] | |
(customer: customer) | |
selector | |
] | |
Meteor.publish 'shots-count', (selector, customer)-> | |
count = 0 | |
uuid = Meteor.uuid() | |
if shotAccessControl @userId(), selector, customer | |
handle = Shots.find(shotSelector(selector, customer)).observe | |
added: (doc, idx)=> | |
count++ | |
@set 'shots-count', uuid, count: count | |
@flush() | |
removed: (doc, idx)=> | |
count-- | |
@set 'shots-count', uuid, count: count | |
@flush() | |
# orderly release the observe handle after client disconnects | |
@onStop => | |
handle.stop() | |
@unset 'shots-count', uuid, ["count"] | |
@flush |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment