如何为具有参数和关系的方法编写远程挂钩 [英] How to write remote hook for a method with parameter and relation
问题描述
我经历了远程挂钩文档,我可以成功创建远程钩子,用于没有额外参数的方法,例如login
,
这是:
I went through the remote hook documentation, I can successfully create remote hooks for methods without extra parameters, like login
,
which is:
customer.afterRemote('login', function(ctx, modelInstance, next) {
if (ctx.result) {
...
next();
}
else{
next();
}
});
现在,如何为方法写一个远程钩子:
Now, How to write a remote hook for a method say :
GET /customers/{id}
POST /customers/{id}
或 同时发布相关对象,例如
or while posting related objects like
POST /customers/{id}/contacts
GET /customers/{id}/contacts
我知道对POST /customers/{id}/contacts
执行以下操作:
I know doing the following with POST /customers/{id}/contacts
:
customer.beforeRemote('**', function(ctx, user, next) {
console.log(ctx.methodString, 'was invoked remotely'); // customers.prototype.save was invoked remotely
next();
});
将返回名为的方法的名称,例如:
would return the name of the method called like:
customer.prototype .__ create__contacts被远程调用
customer.prototype.__create__contacts was invoked remotely
但是我仍然无法具体挂钩,并且以下尝试均无济于事,并且无法实现挂钩:
But I am still unable to hook it specifically, and following tries are to no avail, and hook isn't being reached:
customer.beforeRemote('customer.prototype.__create__contacts', function(ctx, user, next)
customer.beforeRemote(customer.prototype.__create__contacts, function(ctx, user, next)
推荐答案
发现了!答案就在此处
首先要使用我刚才提到的customer.beforeRemote('**', function(ctx, user, next)
捕获方法名称,然后只需执行以下操作即可:
First catch the method name using customer.beforeRemote('**', function(ctx, user, next)
as I mentioned in question, then simply following would work:
customer.beforeRemote('*.__create__assets', function(ctx, user, next) {
console.log(ctx.methodString, 'was invoked remotely with customers'); // customers.prototype.save was invoked remotely
next();
});
这篇关于如何为具有参数和关系的方法编写远程挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!