Last active
August 29, 2015 13:59
-
-
Save hellogerard/10995236 to your computer and use it in GitHub Desktop.
Modifying/transforming a Meteor collection cursor in a publication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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