简体   繁体   中英

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?

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? 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?

If you want to avoid massive clicks from a single user, use ddp-rate-limiter package and create a rule for your method. 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? I tried putting it after run, before run, before the whole block...

ValidatedMethod run function works the same way as a method function. So your just need to add this.unblock inside the run function.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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