javascript - 在 Meteor JS 中,ownsDocument 变量中的 "doc"是什么?

标签 javascript meteor

这是来自 Discover Meteor 一书:

lib/permissions.js

// check that the userId specified owns the documents
ownsDocument = function(userId, doc) {
  return doc && doc.userId === userId;
}

collections/posts.js

Posts = new Meteor.Collection('posts');

Posts.allow({
  update: ownsDocument,
  remove: ownsDocument
});

Meteor.methods({
...

所以 ownsDocument 是一个全局变量,它包含一个函数,该函数接受两个参数,userID 和 doc。

稍后,当您在 posts.js 中创建 Posts.allow 哈希时,键“update”和“remove”被分配给 ownsDocument,但没有传入任何参数。

呃...它以某种方式工作。如果没有传入,它怎么知道 userID 和 doc 是什么? “doc”到底指的是什么?

最佳答案

这是一流的功能。您可以看到,好像 Posts.allow 中的 ownsDocument 被整个 ownsDocument 函数替换了:

ownsDocument = function(userId, doc) {
  return doc && doc.userId === userId;
}

Posts.allow({
  update: ownsDocument,
  remove: ownsDocument
});

// Can be seen as equivalent to:

Posts.allow({
  update: function(userId, doc) {
    return doc && doc.userId === userId;
  },
  remove: function(userId, doc) {
    return doc && doc.userId === userId;
  }
});

优点是避免重复自己,如果以后要更新ownsDocument,只需更改一个地方。也可以这样写:

Posts.allow({
  update: function(userId, doc) {
    return ownsDocument(userId, doc);
  },
  remove: function(userId, doc) {
    return ownsDocument(userId, doc);
  }
});

关于javascript - 在 Meteor JS 中,ownsDocument 变量中的 "doc"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23488362/

相关文章:

javascript - meteor /铁路由器 : how to wait on data which depends on other data

javascript - 这个 Redux (react) 应用程序可能存在什么问题?

javascript - 如何提高该功能的性能?

电子邮件地址顶级域的 JavaScript 验证

javascript - 如何从已部署的 Meteor 应用程序的子目录中读取文件?

meteor - 如何将数据从服务器推送到所有不使用集合的客户端?

javascript - 在 three.js 中的球体上使用纹理

javascript - angularJS:用于溢出文本和性能的 dotdotdot

ios - 如何使用本地设备 Facebook 登录 Meteor?

javascript - meteor :图像的预加载