Skip to content

Instantly share code, notes, and snippets.

@lwille
Created September 12, 2012 03:01
Show Gist options
  • Save lwille/3703983 to your computer and use it in GitHub Desktop.
Save lwille/3703983 to your computer and use it in GitHub Desktop.
publish count
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