在您的发布中,您可以使用较低级别的发布 API 来更好地控制结果,而不是返回游标。
例如,当您从出版物返回游标时,Meteor 调用 _publishCursor 函数,如下所示:
Mongo.Collection._publishCursor = function (cursor, sub, collection) {
var observeHandle = cursor.observeChanges({
added: function (id, fields) {
sub.added(collection, id, fields);
},
changed: function (id, fields) {
sub.changed(collection, id, fields);
},
removed: function (id) {
sub.removed(collection, id);
}
});
// We don't call sub.ready() here: it gets called in livedata_server, after
// possibly calling _publishCursor on multiple returned cursors.
// register stop callback (expects lambda w/ no args).
sub.onStop(function () {observeHandle.stop();});
// return the observeHandle in case it needs to be stopped early
return observeHandle;
};
因此,您可以修改您的出版物,基本上做同样的事情,但也可以发布一个文本字段,该字段从描述字段中获取其值,如下所示:
给定以下集合:
MyCollection = new Mongo.Collection("my_collection");
您的发布功能可能如下所示:
Meteor.publish("myPub", function () {
var sub = this;
var observeHandle = myCollection.find().observeChanges({
added: function (id, fields) {
fields.text = fields.description; // assign text value here
sub.added("my_collection", id, fields);
},
changed: function (id, fields) {
fields.text = fields.description; // assign text value here
sub.changed("my_collection", id, fields);
},
removed: function (id) {
sub.removed("my_collection", id);
}
});
sub.ready()
// register stop callback (expects lambda w/ no args).
sub.onStop(function () {observeHandle.stop();});
};