Skip to content

Instantly share code, notes, and snippets.

Created December 17, 2012 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4314731 to your computer and use it in GitHub Desktop.
Save anonymous/4314731 to your computer and use it in GitHub Desktop.
How to synchronize a client-only collection with a shared collection in Meteor. Doesn't handle callbacks other than added, but should do what it's supposed to do.
//common to client and server
SharedCollection = new Meteor.Collection('shared');
//client from here on out
LocalMirror = new Meteor.Collection(null);
var createLocalFromShared = function(doc) {
// create a new id for the local doc, but store the old one
doc.sharedId = doc._id;
delete doc._id;
LocalCollection.insert(doc);
};
// should only run this once, and before a subscription has pulled
// the shared data from the server. Otherwise I'm not sure that
// the added callback will do the initial populate step.
SharedCollection.find().observe({
added: function(doc,beforeIndex) {
if (LocalCollection.findOne({sharedId: doc._id})) {
console.log('already have a local doc with that id...');
return;
}
createLocalFromShared(doc);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment