2013-02-01 8 views
12

पर गिनती की सदस्यता लें उल्का में गिनती की सदस्यता लेने का कोई तरीका है।उल्का

मैं articles.find() प्रकाशित करने के बजाय Articles.find().count() प्रकाशित करना चाहता हूं। आदर्श रूप से इसे एक प्रतिक्रियाशील सत्र में गिनती असाइन करनी चाहिए जो गिनती में परिवर्तन के दौरान बदल जाएगी।

+3

आप इस प्रविष्टि को पढ़ना चाहेंगे: http://stackoverflow.com/questions/10565654/how-does-the-messages-count-example-in-meteor-docs-work – machour

+2

आपके पास है पहले से ही आपके प्रश्न का उत्तर दिया :-) –

उत्तर

16

मैं अपने काउंटरों

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 
+0

एपीएम ब्लॉग पर एक लिंक देखा। अच्छा – Harry

+0

@ हैरी केयर लिंक शामिल करने के लिए? – Choy

+0

@Choy, यहां हैरी लिंक इस बारे में बात कर रहा है: https://kadira.io/academy/reducing-pubsub-data-usage/ –

8

उल्का दस्तावेज वास्तव में अद्यतन निरीक्षण API के साथ ऐसा करने का एक अच्छा उदाहरण दिखाता है। मैं इसे यहां दोबारा पोस्ट कर रहा हूं लेकिन मूल दस्तावेज यहां है: http://docs.meteor.com/#meteor_publish

Meteor.publish("counts-by-room", function (roomId) { 
    var self = this; 
    var count = 0; 
    var initializing = true; 
    var handle = Messages.find({roomId: roomId}).observeChanges({ 
    added: function (id) { 
     count++; 
     if (!initializing) 
     self.changed("counts", roomId, {count: count}); 
    }, 
    removed: function (id) { 
     count--; 
     self.changed("counts", roomId, {count: count}); 
    } 
    // don't care about moved or changed 
    }); 

    // Observe only returns after the initial added callbacks have 
    // run. Now return an initial value and mark the subscription 
    // as ready. 
    initializing = false; 
    self.added("counts", roomId, {count: count}); 
    self.ready(); 

    // Stop observing the cursor when client unsubs. 
    // Stopping a subscription automatically takes 
    // care of sending the client any removed messages. 
    self.onStop(function() { 
    handle.stop(); 
    }); 
}); 

// client: declare collection to hold count object 
Counts = new Meteor.Collection("counts"); 

// client: subscribe to the count for the current room 
Meteor.autorun(function() { 
    Meteor.subscribe("counts-by-room", Session.get("roomId")); 
}); 

// client: use the new collection 
console.log("Current room has " + 
      Counts.findOne(Session.get("roomId")).count + 
      " messages.");