Se si è disposti ad utilizzare il ramo Auth di Meteor, questo è ciò che ho fatto con alcuni commenti aggiunti. Non ero un fan della risposta di Josh perché non mi fido dei clienti! Loro mentono.
In questo esempio, diremo che ogni utente ha un singolo oggetto magico. E ci rifiutiamo di usare qualsiasi informazione che l'utente possa manipolare lato client (cioè variabili di sessione).
sul server:
//Create our database
MagicalObjects = new Meteor.Collection("magicalObjects");
// Publish the magical object for the client
Meteor.publish("get-the-magical-object", function() {
//In the auth branch, server and client have access to this.userId
//And there is also a collection of users server side
var uid = this.userId();
//I make sure that when I make this connection, I've created a magical object
//for each user.
//Let's assume this adds a parameter to magical object for the userId
//it's linked to (i.e. magObject.uid = ~user id~)
//we grab our current user from the users database, and pass to our function
checkUserHasMagicalItem(Meteor.users.findOne({_id: uid}));
var self = this;
console.log('Writing publish');
console.log('uid: ' + this.userId());
var magicalObject = MagicalObjects.findOne({uid: uid});
//Now, I want to know if the magical object is changed -- and update accordingly
//with its changes -- you might not need this part
//If you don't- then just uncomment these two lines, ignore the rest
//self.set("magicObject", uid, {magicalobject: magicalObject});
//self.flush();
//Here, we're going to watch anything that happens to our magical object
//that's tied to our user
var handle = MagicalObjects.find({uid: uid}).observe({
added: function(doc, idx)
{
//get the latest version of our object
magicalObject = MagicalObjects.findOne({uid: uid});
console.log('added object');
//now we set this server side
self.set("magicObject", uid, {magicalobject: magicalObject});
self.flush();
},
//I'm not concerned about removing, but
//we do care if it is changed
changed: function(newDoc, idx, oldDoc)
{
console.log('changed object');
magicalObject = MagicalObjects.findOne({uid: uid});
self.set("magicObject", uid, {magicalobject: magicalObject});
self.flush();
}
//end observe
});
//for when the player disconnects
self.onStop(function() {
console.log('Stopping');
handle.stop();
//end onStop
});
//end publish
});
On Cliente:
//this is the name of our collection client side
MagicalObject = new Meteor.Collection("magicObject");
//notice the name is equal to whatever string you use when you call
//self.set on the server
//notice, this is the name equal to whatever string you use when you
//call Meteor.publish on the server
Meteor.subscribe("get-the-magical-object");
Poi, quando si vuole andare e afferrare il vostro oggetto magico:
var magicObject = MagicalObject.findOne().magicalobject;
Avviso qui che.magicobject NON è un refuso, è il parametro che abbiamo usato in self.set - {magicalobject: magicalObject}.
Mi scuso per la lunga risposta. Ma per concludere rapidamente: cosa abbiamo fatto?
Sul server, abbiamo una collezione di MagicalObjects a cui il cliente non ha accesso. Invece, pubblichiamo un singolo oggetto da oggetti magici - che noi chiamiamo "magicalObject". In base a ciò che impostiamo, ogni oggetto appartiene a un utente. Quindi è un oggetto specifico dell'utente come richiesto dall'op.
Il client crea una raccolta (il cui nome è "magicObject"), quindi riceve i dati quando i dati effettivi nel database del server cambiano. Questa collezione ha un solo oggetto per design, ma quell'oggetto può avere molti parametri (ad esempio magicObject.kazoo o magicalObject.isHarryPotter) oppure è possibile memorizzare molti oggetti diversi (ad esempio nonMagicItem).
+1 su client e server trasparenti Sincronizzazione sessioni. Ho pensato che funzionasse in questo modo, ed era confuso che non funzionasse – 7zark7