【问题标题】:Is there a way to distinguish between custom remote methods and standard remote methods?有没有办法区分自定义远程方法和标准远程方法?
【发布时间】:2020-05-20 05:04:50
【问题描述】:

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

例如我创建一个远程方法如下:

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

Customer.remoteMethod(
// remote method definition
);

现在假设 afterRemote()beforeRemote() 调用(例如在 mixin 中定义),无论如何要确定这是自定义远程方法调用还是标准远程方法调用(如 findfindById等)?

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


    【解决方案1】:

    来自 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;
      // ...
    });
    

    【讨论】:

      猜你喜欢
      • 2017-02-13
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多