javascript - Is it possible to publish items with multiple conditional fields from a collection? -
consider case, have party object, party object has few info fields party, location, time, guests, etc.. has few fields set privacy of info fields, showlocation, showguests, etc.. these hide info users not invited.
how can send client fields "show" field true?
when publish single item can use this: (merge not real function)
meteor.publish("party", function (_id) { var party = parties.findone({_id: _id}); var fields = merge( {name: 1, title: 1, creatoruserid: 1}, (party.showlocationanddate ? {location: 1, date: 1} : null), (party.showguests ? {guests: 1} : null) ); return parties.find({_id: _id}, {fields: fields}); });
when publish multiple items single condition can use this:
meteor.publish("parties", function () { var fieldswithlocation = { name: 1, title: 1, creatoruserid: 1, location: 1 }; var fieldswithoutlocation = { name: 1, title: 1, creatoruserid: 1 }; //return multiple cursors return [ parties.find({showlocation: true}, {fields: fieldswithlocation}), parties.find({showlocation: false}, {fields: fieldswithoutlocation}) ]; });
but how can elegantly few conditional fields when publishing multiple items?
three options:
- define
transform
function when create collection - use cursor.map() on server run callback on every document , include/exclude fields based on requirements. resulting object array instead of cursor client happily use that.
- use cursor.fetch() on server side , process resulting array
foreach()
Comments
Post a Comment