Skip to content

Instantly share code, notes, and snippets.

@boxofrox

boxofrox/lib.js Secret

Last active December 5, 2015 07:39
Show Gist options
  • Save boxofrox/09ee8229fc8183e9f8e6 to your computer and use it in GitHub Desktop.
Save boxofrox/09ee8229fc8183e9f8e6 to your computer and use it in GitHub Desktop.
Example meteor app with collection, no autopublish, counters, and working reactivity.
populateCollection = function populateCollection (collection) {
var choices = [ 'completed', 'in progress' ];
var rec = { status: Random.choice(choices) };
var id = collection.insert(rec);
console.log('insert', rec, 'in collection', collection._name, 'with id', id);
}
allowAll = function allowAll (collection) {
var yes = function () { return true; };
collection.allow({
insert: yes,
update: yes,
remove: yes,
});
}
createRow = function createRowHelper (name, inProgressCounterName, completedCounterName, totalCounterName, notes, insertable, className) {
return {
name: name,
inProgressCounterName: inProgressCounterName,
completedCounterName: completedCounterName,
totalCounterName: totalCounterName,
insertable: insertable,
className: className,
notes: notes,
};
}
/* CSS declarations go here */
<head>
<title>reactivity-demo</title>
</head>
<body>
<h1>Welcome to Meteor!</h1>
{{> collections}}
</body>
<template name="collections">
<table>
<thead>
<tr>
<td>Collection</td>
<td>In Progress</td>
<td>Completed</td>
<td>Total</td>
<td>Action</td>
<td>Notes</td>
</tr>
</thead>
<tbody>
{{#each c in collections}}
{{> countRow c }}
{{/each}}
</tbody>
</table>
</template>
<template name="countRow">
<tr>
<td>{{ name }}</td>
<td>{{ counter (inProgressCounterName) }}</td>
<td>{{ counter (completedCounterName) }}</td>
<td>{{ counter (totalCounterName) }}</td>
{{#if insertable}}
<td>
<button class="{{ className }}">Insert Record</button>
</td>
{{else}}
<td></td>
{{/if}}
<td>{{{ notes }}}</td>
</tr>
</template>
// create collection on both client and server
ThingsA = new Mongo.Collection('things-a'); // populated by server on first run.
ThingsB = new Mongo.Collection('things-b'); // populated by client (browser console).
ThingsC = new Mongo.Collection('things-c'); // populated by server via Meteor.methods rpc.
if (Meteor.isServer) {
Meteor.startup(function () {
[ThingsA, ThingsB, ThingsC].forEach(allowAll);
populateCollection(ThingsA);
Meteor.methods({
populateC: function () {
populateCollection(ThingsC);
},
});
Meteor.publish('things-a', function () {
Counts.publish(this, 'things-a-completed-count', ThingsA.find({ status: { $eq: 'completed' }}));
Counts.publish(this, 'things-a-in-progress-count', ThingsA.find({ status: { $eq: 'in progress' }}));
Counts.publish(this, 'things-a-total-count', ThingsA.find({ status: { $exists: true }}));
// commented to prevent caching of collection in client.
// return ThingsA.find({ status: { $eq: 'completed' }});
});
Meteor.publish('things-b', function () {
Counts.publish(this, 'things-b-completed-count', ThingsB.find({ status: { $eq: 'completed' }}));
Counts.publish(this, 'things-b-in-progress-count', ThingsB.find({ status: { $eq: 'in progress' }}));
Counts.publish(this, 'things-b-total-count', ThingsB.find({ status: { $exists: true }}));
// commented to prevent caching of collection in client.
// return ThingsB.find({ status: { $eq: 'completed' }});
});
Meteor.publish('things-c', function () {
Counts.publish(this, 'things-c-completed-count', ThingsC.find({ status: { $eq: 'completed' }}));
Counts.publish(this, 'things-c-in-progress-count', ThingsC.find({ status: { $eq: 'in progress' }}));
Counts.publish(this, 'things-c-total-count', ThingsC.find({ status: { $exists: true }}));
// commented to prevent caching of collection in client.
// return ThingsC.find({ status: { $eq: 'completed' }});
});
});
}
if (Meteor.isClient) {
Template.countRow.helpers({
counter: function (name) {
return Counts.get(name);
},
});
Template.collections.helpers({
collections: [
createRow(
'Things A',
'things-a-in-progress-count',
'things-a-completed-count',
'things-a-total-count',
'inserts a record when <code>Meteor.startup()</code> runs.',
false
),
createRow(
'Things B',
'things-b-in-progress-count',
'things-b-completed-count',
'things-b-total-count',
'using client function, <code>populateCollection()</code>.',
true,
'insert b'
),
createRow(
'Things C',
'things-c-in-progress-count',
'things-c-completed-count',
'things-c-total-count',
"using <code>Meteor.call('populateC')</code>.",
true,
'insert c'
),
],
});
Template.collections.events({
'click .insert.b': function () {
populateCollection(ThingsB);
},
'click .insert.c': function () {
populateCollection(ThingsC);
},
});
Meteor.subscribe('things-a');
Meteor.subscribe('things-b');
Meteor.subscribe('things-c');
}
#!/bin/bash
meteor create main
declare -a FILES=(
lib.js
main.css
main.html
main.js
)
for file in "${FILES[@]}"; do
echo $file
cp $file main/$file
done
cd main
meteor add random
meteor add tmeasday:publish-counts
meteor remove autopublish
echo <<EOF
Enter project folder and start meteor server.
$ cd main
$ meteor run
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment