是的,pub/sub 可用于任意对象。Meteor 的文档甚至提供了一个示例 http://docs.meteor.com/#meteor_publish:
// server: publish the current size of a collection
Meteor.publish("counts-by-room", function (roomId) {
var self = this;
check(roomId, String);
var count = 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 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 changed
});
// Instead, we'll send one `self.added()` message right after
// observeChanges has returned, 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 Mongo.Collection("counts");
// client: subscribe to the count for the current room
Tracker.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.");
在这个例子中,counts-by-room
正在发布从返回的数据创建的任意对象Messages.find()
,但您也可以轻松地在其他地方获取源数据并以相同的方式发布。您只需要提供相同的added
and removed
像这里的例子一样的回调。
您会注意到客户端上有一个名为的集合counts
,但这纯粹是在客户端的内存中;它没有保存在 MongoDB 中。我认为这是使用 pub/sub 所必需的。
如果你想避免仅内存中的集合,你应该看看Meteor.call
。你可以创建一个Meteor.method
like getCountsByRoom(roomId)
并从客户端调用它,例如Meteor.call('getCountsByRoom', 123)
该方法将在服务器上执行并返回其响应。这更像是传统的 Ajax 做事方式,并且您会失去 Meteor 的所有反应性。