मैं अपने काउंटरों
Meteor.publishCounter = (params) ->
count = 0
init = true
id = Random.id()
pub = params.handle
collection = params.collection
handle = collection.find(params.filter, params.options).observeChanges
added: =>
count++
pub.changed(params.name, id, {count: count}) unless init
removed: =>
count--
pub.changed(params.name, id, {count: count}) unless init
init = false
pub.added params.name, id, {count: count}
pub.ready()
pub.onStop -> handle.stop()
प्रकाशित करने के लिए कोड का पालन किया है और मैं इसे इस तरह का उपयोग करें:
Meteor.publish 'bikes-count', (params = {}) ->
Meteor.publishCounter
handle: this
name: 'bikes-count'
collection: Bikes
filter: params
और अंत में ग्राहक पर:
Meteor.subscribe 'bikes-count'
BikesCount = new Meteor.collection 'bikes-count'
Template.counter.count = -> BikesCount.findOne().count
आप इस प्रविष्टि को पढ़ना चाहेंगे: http://stackoverflow.com/questions/10565654/how-does-the-messages-count-example-in-meteor-docs-work – machour
आपके पास है पहले से ही आपके प्रश्न का उत्तर दिया :-) –