如何为具有参数和关系的方法编写远程钩子
我通过远程钩子文档 ,我可以成功创build远程钩子的方法没有额外的参数,如login
,这是:
customer.afterRemote('login', function(ctx, modelInstance, next) { if (ctx.result) { ... next(); } else{ next(); } });
现在,如何写一个方法的远程钩子说:
GET /customers/{id} POST /customers/{id}
或者在发布相关对象时
POST /customers/{id}/contacts GET /customers/{id}/contacts
我知道使用POST /customers/{id}/contacts
:
customer.beforeRemote('**', function(ctx, user, next) { console.log(ctx.methodString, 'was invoked remotely'); // customers.prototype.save was invoked remotely next(); });
会返回所谓的方法的名称,如:
customer.prototype .__ create__contacts被远程调用
但是我仍然无法把它具体挂钩,而下面的尝试是徒劳的,钩子没有达到:
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)
方法名称来捕获方法名称,
customer.beforeRemote('*.__create__assets', function(ctx, user, next) { console.log(ctx.methodString, 'was invoked remotely with customers'); // customers.prototype.save was invoked remotely next(); });