Переопределение удаленного метода отношения в Strongloop Loopback

У меня есть модель пользователя и модель подписчика, которая имеет отношение HasManyThrough с пользователем для подписчика и подписчика. Как изменить параметры метода __get__followers по умолчанию?


person idoshamun    schedule 22.11.2014    source источник


Ответы (2)


Я понял, что могу просто добавить новый удаленный метод обычным способом.

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,
            }
    );
person idoshamun    schedule 22.11.2014

Еще один способ. Дерево имеет много листьев, и мы хотим переопределить обработчик отношения __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)
}
person codepushr    schedule 16.09.2019