Created
February 14, 2013 15:25
-
-
Save matb33/4953521 to your computer and use it in GitHub Desktop.
Example of X new things in Meteor, leveraging local collection
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
<template name="stuff"> | |
<button name="show">Show {{newThingsCount}} new things</button> | |
<ul> | |
{{#each things}} | |
<li>{{title}}</li> | |
{{/each}} | |
</ul> | |
</template> |
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
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