首页 > 解决方案 > 有没有办法区分自定义远程方法和标准远程方法?

问题描述

在 lb v3 中有没有办法区分标准远程方法和自定义远程方法?

例如,我创建了一个远程方法,如下所示:

Customer.order_status = function(orderId, cb) {
// ...
};

Customer.remoteMethod(
// remote method definition
);

现在假设一个afterRemote()orbeforeRemote()调用(例如在 mixin 中定义),是否有办法确定这是自定义远程方法调用还是标准远程方法调用(如find,findById等)?

TargetModel.beforeRemote('**', function(ctx, next) {
let methodString = ctx.methodString;

}

调用的方法字符串order_status类似于Customer.order_status,并且,如果它被定义为非静态方法,它会是Customer.prototype.order_status。现在,我可以测试模型构造函数中的真实性以确定它是否是有效的方法。

例如

!!TargetModel[remoteMethodname] // true if it is a valid static method.

但是,我还没有这些信息。我不知道它是否是定义为自定义远程方法的静态方法。

此外,如果我们在模型上定义了一个范围,它会使事情变得更加复杂。基于上述方法,我无法区分范围远程调用或标准远程调用。

标签: loopbackjs

解决方案


LoopBack 团队的问候

我们不区分 LoopBack 中的内置远程方法和用户提供的远程方法。但是,可以为项目中定义的自定义方法向远程元数据添加一个标志。

例如:

Customer.remoteMethod('foo', {
  // your custom flag
  custom: true,

  // standard remoting metadata
  accepts: [/*...*/],
  returns: [/*...*/],
  // etc.
});

然后在您的远程处理挂钩中,您可以通过 访问远程处理元数据ctx.method.{field},例如:

TargetModel.beforeRemote('**', function(ctx, next) {
  const isCustom = ctx.method.custom;
  // ...
});

推荐阅读