谢谢你提醒我写一个更清楚的解释.下面是一个更完整的例子,并附上我的 comments .我已经清理了一些错误和不一致之处.下一个docs版本将使用这个.
Meteor.publish
是相当灵活的.它不仅限于将现有MongoDB集合发布到客户端:我们可以发布任何我们想要的内容.具体地说,Meteor.publish
定义了客户机可以订阅的set of documents.每个文档都属于某个集合名称(一个字符串),有一个唯一的_id
字段,然后有一组JSON属性.当集合中的文档发生更改时,服务器会将更改发送到每个订阅的客户端,使客户端保持最新.
我们将在这里定义一个名为"counts-by-room"
的文档集,它包含一个名为"counts"
的集合中的单个文档.该文档将有两个字段:带有房间ID的roomId
和count
:该房间中的消息总数.没有真正的名为counts
的MongoDB集合.这只是我们的Meteor服务器将要发送到客户端的集合的名称,并存储在名为counts
的client-side个集合中.
为此,我们的publish函数接受一个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");
然后我们可以订阅.(实际上,你可以在声明Collection 之前订阅:Meteor将对传入的更新进行排队,直到有地方放置它们.)subscription的名称是"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.");