publish subscribe - Meteor Group Collection by Field -
i trying return collection (postings) grouped field (status). pretty new mongo , meteor. query below gives me collections grouped status # of docs status... want same thing have actual documents in there.
also, able publish/subscribe reactivly update. creating admin dashboard groups postings current status.
a friend provided following gist, bit on head: https://gist.github.com/ryw/8827179
db.postings.group({ key: {status: 1}, initial: {sum:0}, reduce: function(doc, prev) { prev.sum += 1; } })
thanks!
if need of documents on client, publish whole collection , let template code group them.
client
tracker.autorun(function() { if (meteor.user()) { meteor.subscribe('allpostings'); } }); template.admin.helpers({ postings: function() { if (session.get('currentstatus')) { return postings.find({status: session.get('currentstatus')}); } }, statuses: function() { return _.uniq(_.pluck(postings.find().fetch(), 'status')); } }); template.admin.events({ 'click .status': function() { session.set('currentstatus', string(this)); } });
<template name="admin"> <div class="left-panel"> <ul> {{#each statuses}} <li class="status">{{this}}</li> {{/each}} </ul> </div> <div class="right-panel"> <ul> {{#each postings}} <li>{{message}}</li> {{/each}} </ul> </div> </template>
server
meteor.publish('allpostings', function() { var user = meteor.users.findone(this.userid); if (user.isadmin) { return postings.find(); } });
i'm assuming have way identify admin users (here used isadmin
). assuming posting has status
, message
.
Comments
Post a Comment