loopbackjs - 有没有办法区分自定义远程方法和标准远程方法?
问题描述
在 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.
但是,我还没有这些信息。我不知道它是否是定义为自定义远程方法的静态方法。
此外,如果我们在模型上定义了一个范围,它会使事情变得更加复杂。基于上述方法,我无法区分范围远程调用或标准远程调用。
解决方案
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;
// ...
});
推荐阅读
- html - 我想使用 CSS 更改类的属性值
- windows - 如何下载和安装 Boost
- python - 如何检测多个屏幕
- cypress - 赛普拉斯如何在页面加载结果与所选内容相同后断言
- c# - C# 如果我需要另一个类中的位图,请使用 Dispose()
- apache - Apache 虚拟主机 -- 如何设置子域以指向游戏服务器
- android - Beacon beacon = new Beacon.Builder () .setId1 (sysid) .setId2 (IdUser) Android Beacon Library 中发布和扫描IdUser时出错
- python - 烧瓶中 URL 中没有 cookie 和 sid 的双向通信
- html - 在 Heroku 上部署时 CSS 无法正确应用
- python-3.x - 内核中 printf 的 PyOpenCL 问题