流星允许规则

时间:2013-01-24 08:50:58

标签: meteor rules

我对流星派对的例子有疑问。

如果我拨打此代码:

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
  }

我希望有人可以向我解释允许功能吗?谢谢!

2 个答案:

答案 0 :(得分:5)

要了解允许/拒绝,您需要了解userIddoc参数的来源。 (正如在任何函数定义中一样,实际的参数名称无关紧要。)只需查看缔约方插入示例:

Parties.allow({

  insert: function (userId, party) {
     return false; // no cowboy inserts -- use createPage method
  }

});

party参数是要插入的文档:

Parties.insert(doc);

如果您正在使用Meteor Accounts身份验证系统,则会自动设置userId参数。否则,您必须自己在服务器上进行设置。你是怎么做到的?

通常,您使用Meteor.call()从客户端调用服务器上的代码。由于没有内置API来设置userId(除了Accounts),你必须自己编写(在你的服务器代码中):

Meteor.methods({

   setUserId: function(userId) {
       this.setUserId(userId);
   }

});

然后您可以在客户端代码中的任何位置调用它:

Meteor.call('setUserId', userId);

答案 1 :(得分:0)

1)变量useriD和party是在哪里定义的?无处!目的是没有用户可以调用此功能。

这是为了从可以使用控制台手动插入新方的用户处获取数据库。记住Meteor在客户端和服务器中复制数据库。

任何用户都可以通过控制台手动插入新的一方。这可以。但是,服务器会拒绝插入,因为它是不允许的。

2)这些是我在方法Meteor.call("createParty", variable1, variable2)中调用的变量吗?是的,变量可用,但此代码未使用正确的定义:

 Meteor.methods({
    createParty: function (options) {

然后将其用作

 Meteor.call('createParty', 
            { title: title, public: public, ... }, // options array!!
            function (error, party) { ... }  // function executed after the call
 );

对你有帮助吗?