我对流星派对的例子有疑问.
如果我调用此代码:
Parties.allow({
insert: function () {
return true;
},
remove: function (){
return true;
},
update: function() {
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
每个人都可以插入,删除和更新.该示例中的代码是
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;
});
}
});
Run Code Online (Sandbox Code Playgroud)
所以我的问题是变量useriD和party在这一行的例子
insert: function (userId, party) {
Run Code Online (Sandbox Code Playgroud)
定义了吗?这些是我在方法中调用的变量
Meteor.call("createParty", variable1, variable2)
Run Code Online (Sandbox Code Playgroud)
?但这是没有意义的,因为客户打电话
Meteor.call('createParty', {
title: title,
description: description,
x: coords.x,
y: coords.y,
public: public
}
Run Code Online (Sandbox Code Playgroud)
我希望有人可以向我解释允许功能吗?谢谢!
要理解允许/拒绝,您需要了解userId和doc参数的来源.(正如在任何函数定义中一样,实际的参数名称无关紧要.)仅查看缔约方插入示例:
Parties.allow({
insert: function (userId, party) {
return false; // no cowboy inserts -- use createPage method
}
});
Run Code Online (Sandbox Code Playgroud)
该party参数是真实插入的文档:
Parties.insert(doc);
Run Code Online (Sandbox Code Playgroud)
userId如果您正在使用Meteor Accounts身份验证系统,则会自动设置该参数.否则,您必须自己在服务器上进行设置.你是怎样做的?
通常,您可以使用从客户端调用服务器上的代码Meteor.call().由于没有内置API来设置userId(除了Accounts),你必须自己编写(进入你的服务器代码):
Meteor.methods({
setUserId: function(userId) {
this.setUserId(userId);
}
});
Run Code Online (Sandbox Code Playgroud)
然后,您可以在客户端代码中的任何位置调用它:
Meteor.call('setUserId', userId);
Run Code Online (Sandbox Code Playgroud)
1)变量 useriD 和 party 是在哪里定义的?无处!目的是没有用户可以调用此函数。
这是为了保护数据库免受可能使用控制台手动插入新方的用户的影响。请记住,Meteor 在客户端和服务器中复制数据库。
任何用户都可以通过控制台手动插入新方。这可以。但是服务器会拒绝插入,因为这是不允许的。
2)这些是我在方法中调用的变量吗Meteor.call("createParty", variable1, variable2)?是的,变量可用,但此代码未使用正确的定义,即:
Meteor.methods({
createParty: function (options) {
Run Code Online (Sandbox Code Playgroud)
之后它被用作
Meteor.call('createParty',
{ title: title, public: public, ... }, // options array!!
function (error, party) { ... } // function executed after the call
);
Run Code Online (Sandbox Code Playgroud)
对你有帮助吗?
| 归档时间: |
|
| 查看次数: |
5901 次 |
| 最近记录: |