Skip to content

Instantly share code, notes, and snippets.

@hellogerard
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellogerard/10995236 to your computer and use it in GitHub Desktop.
Save hellogerard/10995236 to your computer and use it in GitHub Desktop.
Modifying/transforming a Meteor collection cursor in a publication
# Our minimongoid 'List' class, wrapping the 'lists' Meteor.Collection
# (runs on server & client). If not using minimongoid, you could simply
# on the server & client use:
#
# Lists = new Meteor.Collection 'lists'
class @List extends Minimongoid
@_collection: new Meteor.Collection 'lists'
# Insert into contributed list publication a 'contributed' flag, so we can separate
# it on the client from favorite, etc lists.
Meteor.publish 'contributedLists', (username) ->
items = Item.where itemUsername: username
listIds = _.uniq(_.pluck(items, 'listId'))
parms =
_id:
$in: listIds
handle = List.find(parms).observeChanges
added: (id, fields) =>
fields.contributed = true
# NB: 'lists' is the name of the Meteor.Collection, not the publication
@added 'lists', id, fields
changed: (id, fields) =>
fields.contributed = true
@changed 'lists', id, fields
removed: (id, fields) =>
@removed 'lists', id
@ready()
@onStop -> handle.stop()
# Subscribe to the publication 'contributedLists', which will bring down
# contributed lists and merge them with 'lists' Meteor.Collection.
Router.map ->
@route 'listsIndex',
path: '/lists'
waitOn: -> [
Meteor.subscribe 'lists', Session.get('username')
Meteor.subscribe 'contributedLists', Session.get('username')
Meteor.subscribe 'favoritedLists'
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment