Skip to content

Instantly share code, notes, and snippets.

@matb33
Created February 14, 2013 15:25
Show Gist options
  • Save matb33/4953521 to your computer and use it in GitHub Desktop.
Save matb33/4953521 to your computer and use it in GitHub Desktop.
Example of X new things in Meteor, leveraging local collection
<template name="stuff">
<button name="show">Show {{newThingsCount}} new things</button>
<ul>
{{#each things}}
<li>{{title}}</li>
{{/each}}
</ul>
</template>
var Things = new Meteor.Collection("things");
var LocalThings, processed = [];
if (Meteor.isServer) {
Meteor.publish("things", function () {
return Things.find();
});
}
if (Meteor.isClient) {
LocalThings = new Meteor.Collection(null);
Meteor.subscribe("things");
Meteor.autorun(function () {
var things = Things.find();
things.forEach(function (thing) {
var cloned;
if (processed.indexOf(thing._id) === -1) {
processed.push(thing._id);
cloned = _.clone(thing);
delete cloned._id;
cloned.new = true;
LocalThings.insert(cloned);
}
});
});
Template.stuff.things = function () {
return LocalThings.find({new: false});
};
Template.stuff.newThingsCount = function () {
return LocalThings.find({new: true}).count();
};
Template.stuff.events({
"click button[name='show']": function (evt, template) {
LocalThings.update({new: true}, {$set: {new: false}});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment