【问题标题】:wrapMethod Meteor methodswrapMethod 流星方法
【发布时间】:2015-06-11 16:49:09
【问题描述】:

我正在查看此演示文稿building large meteor applications,我喜欢 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');

但是我收到了这个错误。

TypeError: Object # has no method 'wrapMethod'

我遗漏了一些我知道的东西,但找不到任何关于这个"wrapMethod"的信息

更新

也可以试试

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

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

EX.Accounts.Methods 明确无方法。

【问题讨论】:

    标签: javascript methods meteor


    【解决方案1】:

    我认为开发人员在他的 PB 对象上创建了方法 wrapMethod。你可以see here 在 Meteor 中没有任何东西叫wrapMethod。我猜他们写的是这样的:

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

    我认为它有点整洁。 顺便说一句:如您所见,我喜欢命名我的匿名函数。使调试更好。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-09-24
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多