Skip to content

Instantly share code, notes, and snippets.

@yeputons
Created February 21, 2014 17:40
Show Gist options
  • Save yeputons/9139098 to your computer and use it in GitHub Desktop.
Save yeputons/9139098 to your computer and use it in GitHub Desktop.
Test application for meteor-publish-composite
{
"packages": {
"publish-composite": {}
}
}
<head>
<title>temp-app</title>
</head>
<body>
{{> main}}
</body>
<template name="main">
<h3>Items</h3>
<ul>
{{#each items}}
{{> item}}
{{/each}}
</ul>
<h3>Authors</h3>
<ul>
{{#each authors}}
<li>{{id}}, {{_id}}</li>
{{/each}}
</ul>
</template>
<template name="item">
<li><input type="checkbox" id="enabled-{{_id}}" {{#if enabled}}checked{{/if}} /><label for="enabled-{{_id}}">{{_id}}, authorId={{authorId}}</label></li>
</template>
Items = new Meteor.Collection('items');
Authors = new Meteor.Collection('author');
if (Meteor.isClient) {
Template.main.helpers({
items: function() {
return Items.find();
},
authors: function() {
return Authors.find();
}
});
Template.item.events({
'change input': function(ev, instance) {
Items.update(this._id, {$set: {enabled: !!ev.originalTarget.checked}});
}
});
Meteor.subscribe('items-disabled');
Meteor.subscribe('items-enabled');
}
if (Meteor.isServer) {
if (Items.find().count() == 0) {
var ids = [];
for (var i = 0; i < 10; i++) {
ids.push(Authors.insert({id: i}));
}
for (var i = 0; i < 10; i++) {
var a = Math.floor(Math.random() * (10 - 1e-9));
Items.insert({author: ids[a], authorId: a, enabled: false});
}
}
Meteor.publish('items-disabled', function() {
return Items.find({enabled: false});
});
Meteor.publishComposite('items-enabled', {
find: function() {
return Items.find({enabled: true});
},
children: [
{
find: function(item) {
return Authors.find(item.author);
}
}
]
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment