如果你愿意使用Meeteor的Austh分公司,这是我用一些补充意见所做的。 由于我不相信客户! 他们是谎言。
在这个例子中,我们可以说,每个用户都有一个单一魔术物体。 我们拒绝使用用户能够操纵客户方面的任何信息(即会议变量)。
在服务器上:
//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");
那么,当你们想去做和 gr笑你们的魔术:
var magicObject = MagicalObject.findOne().magicalobject;
这里通知说,神灵是NOT的典型,它是我们自用的参数。 页: 1
我对长期回答表示歉意。 但是,要迅速总结:我们做了些什么?
在服务器上,我们收集了客户确实能够查阅的门槛物。 相反,我们从魔法中发表了一个单一物体——我们称之为“魔法” 根据我们确定的情况,每个物体都属于一个用户。 因此,它按照同前引书的要求成为用户特定物体。
客户创建收集(其名称为“磁性目标”),然后在服务器数据库的实际数据发生变化时发送数据。 这种收集只设计一个物体,但该物体可具有许多参数(例如:魔法、kazoo或魔法、isHarryPotter),或可以储存许多不同的物体(例如非MagicItem)。