I want to make a publication with several additional fields, but I don't want to either use Collection.aggregate
and lose my publication updates when the collection change (so I can't just use self.added
in it either).
I plan to use Cursor.observeChanges
in order to achieve that. I have two major constraints:
- I don't want to publish all the documents fields
- I want to use some of the unpublished fields to create new ones. For example, I have a field
item
where I store an array ofitem
_id. I don't want to publish it, but I want to publish aitem_count
field with the length of my field array
Here comes the approach:
I plan to chain find queries. I never did that so I wonder if it possible. The general (simplified) query structure would be like this: http://jsfiddle.net/Billybobbonnet/1cgrqouj/ (I cant get the code properly displayed here)
Based on the count example in Meteor documentation, I store my query in a variable
handle
in order to stop the changes notification if a client unsubscribes:
self.onStop(function () {
handle.stop();
});
- I add a flag
initializing = true;
before my query and I set it totrue
just before callingself.ready();
. I use this flag to change myitemCount
variable only if it is the publication is initialized. So basically, I change myswitch
like that:
switch (field) {
case "item"
if (!initializing)
itemCount = raw_document.item.length;
break;
default:
}
I wanted to check that this approach is good and possible before committing into big changes in my code. Can someone confirm me if this is the right way to go?
It's relatively easy to keep fields private even if they are part of the database query. The last argument to
self.added
is the object being passed to the client, so you can strip/modify/delete fields you are sending to the client.Here's a modified version of your fiddle. This should do what you are asking for. (To be honest I'm not sure why you had anything chained after the
observeChanges
function in your fiddle, so maybe I'm misunderstanding you, but looking at the rest of your question this should be it. Sorry if I got it wrong.)