यदि आप उल्का की औथ शाखा का उपयोग करने के इच्छुक हैं, तो मैंने कुछ अतिरिक्त टिप्पणियों के साथ यही किया है। मैं जोश के जवाब का प्रशंसक नहीं था क्योंकि मुझे ग्राहकों पर भरोसा नहीं है! वे झूठ बोलते हैं।
इस उदाहरण में, हम कहेंगे कि प्रत्येक उपयोगकर्ता के पास एक जादुई वस्तु है। और हम किसी भी जानकारी का उपयोग करने से इनकार करते हैं जो उपयोगकर्ता क्लाइंट साइड (यानी सत्र चर) में हेरफेर कर सकता है।
सर्वर पर:
//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
});
क्लाइंट पर:
//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");
तब, जब तुम जाओ और अपने जादुई वस्तु हड़पने के लिए चाहते हैं:
var magicObject = MagicalObject.findOne().magicalobject;
सूचना है कि यहाँ।magicalobject एक टाइपो नहीं है, यह पैरामीटर है जिसे हम self.set में उपयोग करते हैं - {magicalobject: magicalObject}।
मैं लंबे उत्तर के लिए क्षमा चाहता हूं। लेकिन जल्दी से लपेटने के लिए: हमने क्या किया है?
सर्वर पर, हमारे पास MagicalObjects का संग्रह है जिसे क्लाइंट तक पहुंच नहीं है। इसके बजाए, हम एक वस्तु को जादुई वस्तुओं से प्रकाशित करते हैं - जिसे हम "जादुई ऑब्जेक्ट" कहते हैं। हमने जो सेट अप किया है, उसके अनुसार, प्रत्येक ऑब्जेक्ट एक उपयोगकर्ता से संबंधित है। तो यह ऑप द्वारा अनुरोध किए गए उपयोगकर्ता विशिष्ट ऑब्जेक्ट है।
क्लाइंट एक संग्रह बनाता है (जिसका नाम "जादुई ऑब्जेक्ट" है), और सर्वर डेटाबेस में वास्तविक डेटा बदलते समय डेटा भेज दिया जाता है। इस संग्रह में केवल एक ऑब्जेक्ट डिज़ाइन है, लेकिन उस ऑब्जेक्ट में कई पैरामीटर हो सकते हैं (उदा। MagicalObject.kazoo या magicalObject.isHarryPotter) या आप कई अलग-अलग ऑब्जेक्ट्स (उदा। NonMagicItem) स्टोर कर सकते हैं।
+1। मैंने माना कि यह इस तरह से काम करता है, और उलझन में था कि यह काम नहीं कर रहा था – 7zark7