简体   繁体   English

在ValidatedMethod中流星this.unblock

[英]Meteor this.unblock in ValidatedMethod

I have a validated method: 我有一个经过验证的方法:

export const updateSuitClass = new ValidatedMethod({
  name: 'presets.suitClass.update',
  validate: new SimpleSchema({
    _id: { type: String },
    rankClass: { type: String },
    suitClass: { type: String },
  }).validator(),
  run({ _id, rankClass, suitClass }) {
    const userId = Meteor.userId();
    if (userId) {
      const lookUpPreset = Presets.findOne({ _id });
      if (userId === lookUpPreset.owner) {
        Presets.update(_id, { $set: { [`${rankClass}.${suitClass}`]: !lookUpPreset[rankClass][suitClass] } });
      } else {
        throw new Meteor.Error('not-authorized', 'Please don\'t update another user\'s preset.');
      }
    } else {
      throw new Meteor.Error('not-authorized', 'Please login to update your preset.');
    }
  },
});

that gets called on a click event (on an item in a list and toggles a check mark next to it to indicate checked) to save state of user's configuration settings. 会在点击事件(在列表中的项目上,并在其旁边的选中标记之间切换以指示已选中)上被调用以保存用户配置设置的状态。 Problem is, it gets called as user clicks clicks and clicks so it will get called quite frequently. 问题是,它随着用户的点击和点击而被调用,因此它会被频繁调用。

First question, is it bad to make so many method calls to server to update a portion at a time? 第一个问题,对服务器进行如此多次的调用以一次更新一部分是否不好? Should I just put a save button (ew!) and do a single mass update? 我应该只保存一个按钮(ew!)并进行一次批量更新吗?

Second question, if I were to keep the same method code as is but add a this.unblock or Meteor.defer, how do I do that to a validated method? 第二个问题,如果我保持原样的方法代码,但添加this.unblock或Meteor.defer,我该如何对经过验证的方法进行处理? I tried putting it after run, before run, before the whole block... 我试过将它放在运行后,运行前,整个块之前...

Can you guys help? 你们可以帮忙吗?

First question, is it bad to make so many method calls to server to update a portion at a time? 第一个问题,对服务器进行如此多次的调用以一次更新一部分是否不好? Should I just put a save button (ew!) and do a single mass update? 我应该只保存一个按钮(ew!)并进行一次批量更新吗?

If you want to avoid massive clicks from a single user, use ddp-rate-limiter package and create a rule for your method. 如果要避免单个用户的大量点击,请使用ddp-rate-limiter程序包并为您的方法创建一个规则。 With this package you can limit the calls on the server by a time period. 使用此程序包,您可以在一段时间内限制服务器上的呼叫。

Second question, if I were to keep the same method code as is but add a this.unblock or Meteor.defer, how do I do that to a validated method? 第二个问题,如果我保持原样的方法代码,但添加this.unblock或Meteor.defer,我该如何对经过验证的方法进行处理? I tried putting it after run, before run, before the whole block... 我试过将它放在运行后,运行前,整个块之前...

ValidatedMethod run function works the same way as a method function. ValidatedMethod run函数的工作方式与方法函数相同。 So your just need to add this.unblock inside the run function. 因此,您只需要在run函数中添加this.unblock

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM