我想为某些需要加载某些关联的羽毛服务调用。问题是,当我用钩子做“羽毛方式”时,钩子不是一次,而是坚持下去。有没有办法做“羽毛方式一次性协会”?通话后,挂钩应为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;
});
}
。
目前:
{{1}}
答案 0 :(得分:0)
我一次不完全确定你的意思,但如果你只想在钩子函数之外设置一个标志就可以运行钩子,检查它并跳过钩子,如果它是这样的: / p>
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;
}
}