Skip to content

Instantly share code, notes, and snippets.

@jagill
Created July 31, 2013 23:15
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 jagill/6127081 to your computer and use it in GitHub Desktop.
Save jagill/6127081 to your computer and use it in GitHub Desktop.
An example of a "join" with meteor. It's client-powered, which is fine as long as you don't mind people intentionally choosing to get a different set of items.
###
Add coffeescript, random packages:
$ meteor add coffeescript random
Remove autopublish package:
$ meteor remove autopublish
###
##Set up collections and subscriptions
Items = new Meteor.Collection 'items'
Categories = new Meteor.Collection 'categories'
if Meteor.isServer
Meteor.publish 'active-categories', ->
Categories.find active:true
Meteor.publish 'sale-items', (categoryId)->
Items.find categoryId: categoryId
Meteor.startup ->
if Categories.find().count() == 0
Categories.insert _id:'1', name:'One'
Categories.insert _id:'2', name:'Two'
Categories.insert _id:'3', name:'Three'
if Items.find().count() == 0
Items.insert sku:'A', categoryId:'1'
Items.insert sku:'B', categoryId:'1'
Items.insert sku:'C', categoryId:'2'
Items.insert sku:'D', categoryId:'3'
Meteor.setInterval ->
Categories.update {}, {$set: {active:false}}, multi:true
categoryId = Random.choice ['1', '2', '3']
Categories.update categoryId, {$set: {active:true}}
console.log "Setting category #{categoryId} to active"
, 5000
if Meteor.isClient
Meteor.startup ->
Deps.autorun ->
Meteor.subscribe 'active-categories'
activeCategory = Categories.findOne active:true
Meteor.subscribe 'sale-items', activeCategory?._id
## Presentation code
if Meteor.isClient
Template.hello.helpers
saleItems: ->
Items.find()
<head>
<title>joinTest</title>
</head>
<body>
{{> hello}}
</body>
<template name="hello">
<h1>Hello Sales!</h1>
{{#each saleItems}}
{{sku}}
{{/each}}
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment