我可以覆盖/扩展 Meteor 方法吗?

问题描述:

是否有可能以某种方式覆盖 Meteor 中的方法?或者定义另一个函数,这样both 都会被调用?

Is it possible to somehow override a method in Meteor? Or define another function such that both will get called?

在我的常规代码中:

Meteor.methods(
   foo: (parameters) ->
      bar(parameters)
)

稍后加载的其他地方(例如在 tests 中):

Meteor.methods(
   # override
   foo: (parameters) ->
      differentBehavior(parameters)
      # I could call some super() here
)

所以我希望要么同时执行 bardifferentBehavior 要么只执行 differentBehavior 和一些调用 super() 的可能性代码>.

So I would expect to either have both bar and differentBehavior executed or only differentBehavior and some possibility to call super().

这存在吗?

要覆盖一个方法,在服务器端你可以这样做:

To override a method, on server side you can do:

Meteor.methods({
   'method_name': function () {
       //old method definition
   }
});

Meteor.default_server.method_handlers['method_name'] = function (args) {
    //put your new code here
};

Meteor.default_server.method_handlers['method_name'] 必须包含在方法定义之后.

The Meteor.default_server.method_handlers['method_name'] has to be included after the method definition.

要覆盖一个方法(也称为存根),在客户端你可以这样做:

To override a method (also know as a stub), on client side you can do:

Meteor.connection._methodHandlers['method_name'] = function (args) {
    //put your new code here
};

Meteor.connection._methodHandlers['method_name'] 必须包含在方法定义之后.

The Meteor.connection._methodHandlers['method_name'] has to be included after the method definition.