在应用需要访问原始文档的转换后,我需要限制从发布函数发送到客户端的字段数。
我基本上是在尝试避免向客户端发送潜在的巨大数组,并运行一堆检查以返回一个漂亮的整洁对象以供使用。
这是我现在拥有的功能 - 它可以工作,只是不是我想要的方式,基本上限制了观察功能的字段。有没有办法在观察/变换之后添加投影。
Meteor.publish('network', function() {
var self = this;
// get the user values initially
var user = Meteor.users.findOne(self.userId);
var followingUsers = user.following ? user.following.users || [] : [];
var followingChannels = user.following ? user.following.channels || [] : [];
var transformMedia = function(doc) {
// get the user each time to keep this publication reactive
votesUp = doc.votes ? doc.votes.up || [] : [];
votesDown = doc.votes ? doc.votes.down || [] : [];
favourites = doc.votes ? doc.votes.favourites || [] : [];
doc.userActions = {
votedUp: _.contains(votesUp, doc._id) ? 1 : 0,
votedDown: _.contains(votesDown, doc._id) ? 1 : 0,
isFavourite: _.contains(favourites, doc._id) ? 1 : 0,
played: _.contains(doc.played, self.userId) ? 1 : 0,
};
return doc;
};
var networkQuery = Media.find({
$and: [
{
$and: [
{processedAt: { $exists: true} },
{processedStatus: 'successful'},
{publishStatus: 'published'}
]
},
{
// if created by this user, user they follow or channels they subscribe to
$or: [
{createdBy: self.userId },
{createdBy: { $in: followingUsers} },
{channels: { $in: followingChannels} },
]
}
// TODO : add not banned or trashed once implemented
]
}, mediaModifiers).observe({
added: function(doc) {
self.added('media', doc._id, transformMedia(doc));
},
changed: function(doc, oldDoc) {
self.changed('media', doc._id, transformMedia(doc));
},
removed: function(doc) {
self.removed('media', doc._id, transformMedia(doc));
},
});
self.onStop(function() {
networkQuery.stop();
});
self.ready();
});