【发布时间】:2019-09-16 08:58:06
【问题描述】:
我有一个用户模型和一个追随者模型,它与用户具有 HasManyThrough 关系,用于追随者和被追随者。 如何更改默认的 __get__followers 方法参数?
【问题讨论】:
标签: node.js loopbackjs strongloop
我有一个用户模型和一个追随者模型,它与用户具有 HasManyThrough 关系,用于追随者和被追随者。 如何更改默认的 __get__followers 方法参数?
【问题讨论】:
标签: node.js loopbackjs strongloop
我发现我可以按正常方式添加一个新的远程方法。
loopback.remoteMethod(
UserModel.prototype.getFollows,
{
description: 'Get the users who are followed by the user',
accepts: [
{arg: 'page', type: 'Number', http: {source: 'query'}, required: true}
],
returns: {arg: 'data', type: 'object'},
http: {verb: 'get', path: '/follows'},
isStatic: false,
}
);
【讨论】:
另一种方式。树有很多叶子,我们想要覆盖树的__get__leaves 关系处理程序。
/**
* Before Remote handler
*
* @param {Object} ctx - Context
* @param {Function} next - Callback function
*/
Tree.beforeRemote('**', function (ctx, unused, next) {
if (ctx.method.name === '__get__leaves') {
return Tree.getLeaves(ctx)
}
next()
})
/**
* Override Relation handler
*
* @param {Object} ctx - Context
*/
Tree.getLeaves = (ctx) => {
ctx.res.sendStatus(200)
}
【讨论】: