流星のカウントに登録する方法はありますか?流星数を購読する
Articles.find()を公開するのではなく、Articles.find().count()
を公開します。理想的には、カウントが変更されたときに変化する反応的なセッションにカウントを割り当てる必要があります。
流星のカウントに登録する方法はありますか?流星数を購読する
Articles.find()を公開するのではなく、Articles.find().count()
を公開します。理想的には、カウントが変更されたときに変化する反応的なセッションにカウントを割り当てる必要があります。
私はカウンター
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
実際にMeteorのドキュメントは、更新されたobserve 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.");
tmeasday:publish-counts雰囲気パッケージd oes the job;)
あなたはこのエントリを読むことができます:http://stackoverflow.com/questions/10565654/how-does-the-messages-count-example-in-meteor-docs-work – machour
すでにあなたの質問に答えました:-) –