Created
December 17, 2012 00:42
-
-
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.
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 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