我有一个关于 meteor 派对示例的问题。
如果我调用此代码:
Parties.allow({
insert: function () {
return true;
},
remove: function (){
return true;
},
update: function() {
return true;
}
});
每个人都可以插入、删除和更新。
示例中的代码是
Parties.allow({
insert: function (userId, party) {
return false; // no cowboy inserts -- use createPage method
},
update: function (userId, parties, fields, modifier) {
return _.all(parties, function (party) {
if (userId !== party.owner)
return false; // not the owner
var allowed = ["title", "description", "x", "y"];
if (_.difference(fields, allowed).length)
return false; // tried to write to forbidden field
// A good improvement would be to validate the type of the new
// value of the field (and if a string, the length.) In the
// future Meteor will have a schema system to makes that easier.
return true;
});
},
remove: function (userId, parties) {
return ! _.any(parties, function (party) {
// deny if not the owner, or if other people are going
return party.owner !== userId || attending(party) > 0;
});
}
});
所以我的问题是变量 useriD 和 party 在这一行的位置,例如
insert: function (userId, party) {
被定义?
这些是我在方法中调用的变量吗
Meteor.call("createParty", variable1, variable2)
?但这没有意义,因为客户端调用
Meteor.call('createParty', {
title: title,
description: description,
x: coords.x,
y: coords.y,
public: public
}
我希望有人可以向我解释允许功能吗?谢谢!
最佳答案
要了解允许/拒绝,您需要了解 userId
的位置和 doc
参数来自。 (就像在任何函数定义中一样,实际参数名称无关紧要。)只看 Party 插入示例:
Parties.allow({
insert: function (userId, party) {
return false; // no cowboy inserts -- use createPage method
}
});
party
参数是正在插入的文档:Parties.insert(doc);
userId
如果您使用的是 Meteor Accounts 身份验证系统,则参数会自动设置。否则,您必须自己在服务器上进行设置。你是怎样做的?通常,您可以使用
Meteor.call()
从客户端调用服务器上的代码。 .由于没有内置 API 来设置 userId(帐户除外),因此您必须编写自己的(在您的服务器代码中):Meteor.methods({
setUserId: function(userId) {
this.setUserId(userId);
}
});
然后你可以在你的客户端代码的任何地方这样调用它:
Meteor.call('setUserId', userId);
关于 meteor 允许规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497226/