0

我想为需要加载一些关联的某些羽毛服务调用。问题是,当我使用钩子以“羽毛方式”进行操作时,钩子不是一次性的,而是继续使用。有没有办法做一个“羽毛方式一次性关联”?钩子应该destroyed在调用之后。

目前:

serviceAccountAssosiations() {
    return function (hook) {
      const walletModel = hook.app.service('wallets').Model;
      const currencyModel = hook.app.service('currencies').Model;
      const assetClassModel = hook.app.service('asset-classes').Model;
      const exchangeModel = hook.app.service('exchanges').Model;
      const bankModel = hook.app.service('banks').Model;
      const valueAccountModel = hook.app.service('value-accounts').Model
      const valueAccountTransactionModel = hook.app.service('value-account-transactions').Model
      const walletTransactionsModel = hook.app.service('wallet-transactions').Model
      const association = {
        include: [
          { model: exchangeModel },
          { model: bankModel },
          { model: valueAccountModel, include: [{ model: valueAccountTransactionModel },{ model: currencyModel }] },
          { model: walletModel, include: [{ model: walletTransactionsModel },{ model: currencyModel },{ model: assetClassModel }] }
        ]
      };

      switch (hook.type) {
        case 'before':
          hook.params.sequelize = Object.assign(association, { raw: false });
          return Promise.resolve(hook);
          break;

        case 'after':
          dehydrate( association ).call(this, hook);
          break;
      }
    }
  }

get (id, params) {
    console.log('id', id)
    let app = this.options.app;
    return app.service('service-accounts').hooks({
      before: {
        all: [this.serviceAccountAssosiations()]
      },
      after: {
        all: [this.serviceAccountAssosiations()]
      }
    })
    .get(id)
    .then((data) => {
      return data;
    });
  }
4

1 回答 1

0

我不完全确定您的意思,但是如果您只想在可以在钩子函数之外设置一个标志后运行钩子,请检查它并跳过钩子,如果它是这样的:

serviceAccountAssosiations() {
  let ran = false;

  return function (hook) {
    if(ran) {
      return hook;
    }

    const walletModel = hook.app.service('wallets').Model;
    const currencyModel = hook.app.service('currencies').Model;
    const assetClassModel = hook.app.service('asset-classes').Model;
    const exchangeModel = hook.app.service('exchanges').Model;
    const bankModel = hook.app.service('banks').Model;
    const valueAccountModel = hook.app.service('value-accounts').Model
    const valueAccountTransactionModel = hook.app.service('value-account-transactions').Model
    const walletTransactionsModel = hook.app.service('wallet-transactions').Model
    const association = {
      include: [
        { model: exchangeModel },
        { model: bankModel },
        { model: valueAccountModel, include: [{ model: valueAccountTransactionModel },{ model: currencyModel }] },
        { model: walletModel, include: [{ model: walletTransactionsModel },{ model: currencyModel },{ model: assetClassModel }] }
      ]
    };

    switch (hook.type) {
      case 'before':
        hook.params.sequelize = Object.assign(association, { raw: false });
        return Promise.resolve(hook);
        break;

      case 'after':
        dehydrate( association ).call(this, hook);
        break;
    }

    ran = true;

    return hook;
  }
}
于 2017-09-02T23:22:07.113 回答