Skip to content

Instantly share code, notes, and snippets.

@fallen90
Forked from belisarius222/Meteor local mirror
Created March 26, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fallen90/e3eaaaad117c3c1d87080e4476dac520 to your computer and use it in GitHub Desktop.
Save fallen90/e3eaaaad117c3c1d87080e4476dac520 to your computer and use it in GitHub Desktop.
Added `changed` callback and factored out shared --> local conversion.
//common to client and server
SharedCollection = new Meteor.Collection('shared');
//client from here on out
LocalMirror = new Meteor.Collection(null);
var convertSharedToLocal = function(sharedDoc) {
var localDoc = LocalCollection._deepcopy(sharedDoc); // undocumented API, might change
// create a new id for the local doc, but store the old one
localDoc.sharedId = localDoc._id;
delete localDoc._id;
};
// 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;
}
var localDoc = convertSharedToLocal(doc);
LocalCollection.insert(localDoc);
},
changed: function(newDoc) {
var localDoc = convertSharedToLocal(newDoc);
LocalCollection.update({ sharedId: localDoc.sharedId }, localDoc);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment