我指的是这里给出的例子: docs.meteor.com/#publish_added 我已经阅读了答案: How does the messages-count example in Meteor docs work?
我想跟踪3个独立集合中的条目数。将值存储在名为counts的集合中。与示例不同,我不需要过滤表格(通过roomId)。
我在服务器上的:
Meteor.publish("counts-all", function () {
var self = this;
var pcount = 0;
var fcount = 0;
var scount = 0;
var initializing = true;
// observeChanges only returns after the initial `added` callbacks
// have run. Until then, we don't want to send a lot of
// `self.changed()` messages - hence tracking the
// `initializing` state.
var phandle = Persons.find({}).observeChanges({
added: function (id) {
pcount++;
if (!initializing)
self.changed('counts', 'persons', {count: pcount});
// self.changed('counts', 0, {count: pcount});
},
removed: function (id) {
pcount--;
self.changed('counts', 'persons', {count: pcount});
// self.changed('counts', 0, {count: pcount});
}
// don't care about changed
});
var fhandle = Families.find({}).observeChanges({
added: function (id) {
fcount++;
if (!initializing)
self.changed('counts', 'families', {count: fcount});
// self.changed('counts', 1, {count: fcount});
},
removed: function (id) {
fcount--;
self.changed('counts', 'families', {count: fcount});
// self.changed('counts', 1, {count: fcount});
}
// don't care about changed
});
var shandle = Sources.find({}).observeChanges({
added: function (id) {
scount++;
if (!initializing)
self.changed('counts', 'sources', {count: scount});
// self.changed('counts', 2, {count: scount});
},
removed: function (id) {
scount--;
self.changed('counts', 'sources', {count: scount});
// self.changed('counts', 2, {count: scount});
}
// don't care about changed
});
// Instead, we'll send one `self.added()` message right after
// observeChanges has returned, and mark the subscription as
// ready.
console.log('publish counts: '+pcount+' '+fcount+' '+scount);
initializing = false;
self.added('counts', 'persons', {count: pcount});
self.added('counts', 'families', {count: fcount});
self.added('counts', 'sources', {count: scount});
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 () {
phandle.stop();
fhandle.stop();
shandle.stop();
});
});
我可以从我的日志记录中看到它确实计算出每个集合的记录总数: 发表人数:168 150 36
在客户端,我有:
Counts = new Meteor.Collection("counts");
Deps.autorun(function () {
console.log('inside autorun');
Meteor.subscribe("counts-all");
console.log(Counts.find({}));
});
日志记录不会显示集合中的条目。我如何在客户端上引用这些项目? Counts.findOne({_ id:'persons'})。count
此外,在我提到的问题中,网站示例和响应之间存在差异:
指导将不胜感激
答案 0 :(得分:0)
您可以通过以下方式访问客户端counts
:
Deps.autorun(function () {
console.log('inside autorun');
Meteor.subscribe("counts-all");
console.log(Counts.find({_id:"persons"}).fetch());
var persons = Counts.findOne({_id:"persons"}) || {};
var sources = Counts.findOne({_id:"sources"}) || {} ;
var families = Counts.findOne({_id:"families"}) || {};
console.log(persons.count, sources.count, families.count);
});
在v0.9.1中引入了{p> Mongo.collection vs Meteor.collection
和Deps -> Tracker
更改。