有没有办法区分自定义远程方法和标准远程方法?
Is there a way to distinguish between custom remote methods and standard remote methods?
在 lb v3 中有没有办法区分标准远程方法和自定义远程方法?
例如我创建一个远程方法如下:
Customer.order_status = function(orderId, cb) {
// ...
};
Customer.remoteMethod(
// remote method definition
);
现在假设一个 afterRemote()
或 beforeRemote()
调用(例如在 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;
// ...
});
在 lb v3 中有没有办法区分标准远程方法和自定义远程方法?
例如我创建一个远程方法如下:
Customer.order_status = function(orderId, cb) {
// ...
};
Customer.remoteMethod(
// remote method definition
);
现在假设一个 afterRemote()
或 beforeRemote()
调用(例如在 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;
// ...
});