-
-
Save arturcarvalho/8672836 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//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