无法完全理解 the docs 中的这个示例...我尝试了很多不同的方式来运行它,以便观察它是如何工作的,等等。
你如何订阅这个?我们可以包含完成这项工作所需的客户端代码吗?
是否有一个名为 messages-count
的集合? Room
是消息的集合吗?我们可以在示例中包含集合定义吗?
任何关于这方面的提示都会很棒!
注意:这是最初发布此问题时出现的代码(2012 年 5 月)。现在更简单了。
// server: publish the current size of a collection
Meteor.publish("messages-count", function (roomId) {
var self = this;
var uuid = Meteor.uuid();
var count = 0;
handle = Room.find({room_id: roomId}).observe({
added: function (doc, idx) {
count++;
self.set("messages-count", uuid, "count", count);
self.flush();
},
removed: function (doc, idx) {
count--;
self.set("messages-count", uuid, "count", count);
self.flush();
}
// don't care about moved or changed
});
// remove data and turn off observe when client unsubs
self.onStop(function () {
handle.stop();
self.unset("messages-count", uuid, "count");
self.flush();
});
});
最佳答案
感谢您促使我写出更清晰的解释。这是我评论的更完整示例。我已经清理了一些错误和不一致之处。下一个文档版本将使用它。
Meteor.publish
相当灵活。它不仅限于向客户端发布现有的 MongoDB 集合:我们可以发布任何我们想要的内容。具体来说,Meteor.publish
定义了客户端可以订阅的文档集。每个文档都属于某个集合名称(一个字符串),具有唯一的 _id
字段,然后有一些 JSON 属性。随着集合中的文档发生更改,服务器会将更改发送到每个订阅的客户端,使客户端保持最新。
我们将在这里定义一个文档集,称为 "counts-by-room"
,它包含名为 "counts"
的集合中的单个文档.该文档将有两个字段:roomId
带有房间 ID 和 count
:该房间的消息总数。没有名为 counts
的真正 MongoDB 集合.这只是我们的 Meteor 服务器将发送到客户端的集合的名称,并存储在名为 counts
的 client-side 集合中.
为此,我们的发布函数采用 roomId
来自客户端的参数,并观察该房间中所有消息(在其他地方定义)的查询。我们可以使用更高效的observeChanges
在这里观察查询的形式,因为我们不需要完整的文档,只需要添加或删除新文档的知识。任何时候使用 roomId
添加新消息我们感兴趣的是,我们的回调会增加内部计数,然后使用更新后的总数向客户端发布一个新文档。当消息被删除时,它会减少计数并将更新发送给客户端。
当我们第一次调用 observeChanges
时, 一些 added
对于每条已经存在的消息,回调将立即运行。然后,无论何时添加或删除消息,都会触发 future 的更改。
我们的发布函数还注册了一个 onStop
处理程序在客户端取消订阅(手动或断开连接)时进行清理。此处理程序从客户端删除属性并拆除正在运行的 observeChanges
.
每次新客户端订阅 "counts-by-room"
时都会运行发布函数,所以每个客户端都会有一个 observeChanges
代表它运行。
// server: publish the current size of a collection
Meteor.publish("counts-by-room", function (roomId) {
var self = this;
var count = 0;
var initializing = true;
var handle = Messages.find({room_id: roomId}).observeChanges({
added: function (doc, idx) {
count++;
if (!initializing)
self.changed("counts", roomId, {count: count}); // "counts" is the published collection name
},
removed: function (doc, idx) {
count--;
self.changed("counts", roomId, {count: count}); // same published collection, "counts"
}
// don't care about moved or changed
});
initializing = false;
// publish the initial count. `observeChanges` guaranteed not to return
// until the initial set of `added` callbacks have run, so the `count`
// variable is up to date.
self.added("counts", roomId, {count: count});
// and signal that the initial document set is now available on the client
self.ready();
// turn off observe when client unsubscribes
self.onStop(function () {
handle.stop();
});
});
现在,在客户端,我们可以将其视为典型的 Meteor 订阅。首先,我们需要一个 Mongo.Collection
这将保存我们计算出的计数文件。由于服务器正在发布到名为 "counts"
的集合中, 我们通过 "counts"
作为 Mongo.Collection
的参数构造函数。
// client: declare collection to hold count object
Counts = new Mongo.Collection("counts");
然后我们可以订阅。 (实际上,您可以在声明集合之前订阅:Meteor 会将传入的更新排队,直到有地方放置它们。)订阅的名称是 "counts-by-room"
, 它有一个参数:当前房间的 ID。我已经把这个包裹在 Deps.autorun
从而作为 Session.get('roomId')
更改后,客户端将自动退订旧房间的计数并重新订阅新房间的计数。
// client: autosubscribe to the count for the current room
Tracker.autorun(function () {
Meteor.subscribe("counts-by-room", Session.get("roomId"));
});
最后,我们得到了 Counts
中的文档我们可以像使用客户端上的任何其他 Mongo 集合一样使用它。每当服务器发送新计数时,引用此数据的任何模板都会自动重绘。
// client: use the new collection
console.log("Current room has " + Counts.findOne().count + " messages.");
https://stackoverflow.com/questions/10565654/