包装方法 流星方法

wrapMethod Meteor methods

本文关键字:方法 流星 包装      更新时间:2023-09-26

我正在研究这个演示文稿,构建大型流星应用程序,我喜欢wrapMethod()的想法,但似乎我不能像在示例中那样使用它。

这是我的代码。

Meteor.methods({
  'EX.Accounts.Methods.updateProfileData' : function(userId, firstName, secondName) {
    check([firstName, secondName], [String]);
    Meteor.users.update(userId, {
      $set: {
        'profile.firstName': firstName,
        'profile.lastName': secondName,
        'profile.isRegisted': true
      }
    });
  }
});
EX.Accounts.Methods.updateUserProfile =   EX.wrapMethod('EX.Accounts.Methods.updateProfileData');

但是我得到了这个错误。

类型错误:对象 # 没有方法"wrapMethod"

我错过了一些我知道的东西,但找不到有关此"wrapMethod"的任何信息

更新

也试试

_.extend(EX.Accounts.Methods,{
  updateUserProfile : EX.Accounts.Methods.updateProfileData
});

这不会返回错误,但我在全局命名空间上看不到该方法。

前任。Accounts.Methods 很清楚,没有方法。

我认为开发人员在他的PB obejct上创建了wrapMethod的方法。正如你在这里看到的,流星中没有所谓的wrapMethod。我猜他们写了这样的东西:

PB.wrapMethod = function wrapMethod (meteorMethod) {
  return function wrappedMeteorMethod (/*arugments*/) {
    Meteor.apply(meteorMethod, arguments)
  }
}

我觉得挺整齐的。顺便说一句:正如你所看到的,我喜欢命名我的匿名函数。使调试更好。

在 ES6 中,这变成了一种美:

wrapMethod(method) {
    return (...args) => Meteor.call(method, ...args);
}