在 sails.js 中整理助手
Organize helper in sails.js
在 sails.js v1.0 的帮助程序中是否有构建代码的方法?
目前我有类似的东西:
module.exports = {
friendlyName: 'Example helper that does three things',
description: '',
inputs: {},
exits: {},
fn: async function (inputs, exits) {
const doFirstThing = function () {
// do something
};
const doSecondThing = function () {
// do something
};
const doThirdThing = function () {
// do something
};
doFirstThing();
doSecondThing();
doThirdThing();
},
};
三个函数doFirstThing
doSecondThing
doThirdThing
各有15行代码左右。现在,代码很难阅读。有没有办法将函数放在 fn 函数下面或以任何其他更具可读性的方式构建它?
您始终可以在外部定义函数,稍后再将其分配给您的 module.exports
对象。如果不使用doEverything
函数
的闭包变量,也可以单独定义doFirstThing
和其他两个函数
async function doEverything(inputs, exits) {
const doFirstThing = function () {
// do something
};
const doSecondThing = function () {
// do something
};
const doThirdThing = function () {
// do something
};
doFirstThing();
doSecondThing();
doThirdThing();
}
module.exports = {
friendlyName: 'Example helper that does three things',
description: '',
inputs: {},
exits: {},
fn: doEverything
};
在 sails.js v1.0 的帮助程序中是否有构建代码的方法?
目前我有类似的东西:
module.exports = {
friendlyName: 'Example helper that does three things',
description: '',
inputs: {},
exits: {},
fn: async function (inputs, exits) {
const doFirstThing = function () {
// do something
};
const doSecondThing = function () {
// do something
};
const doThirdThing = function () {
// do something
};
doFirstThing();
doSecondThing();
doThirdThing();
},
};
三个函数doFirstThing
doSecondThing
doThirdThing
各有15行代码左右。现在,代码很难阅读。有没有办法将函数放在 fn 函数下面或以任何其他更具可读性的方式构建它?
您始终可以在外部定义函数,稍后再将其分配给您的 module.exports
对象。如果不使用doEverything
函数
doFirstThing
和其他两个函数
async function doEverything(inputs, exits) {
const doFirstThing = function () {
// do something
};
const doSecondThing = function () {
// do something
};
const doThirdThing = function () {
// do something
};
doFirstThing();
doSecondThing();
doThirdThing();
}
module.exports = {
friendlyName: 'Example helper that does three things',
description: '',
inputs: {},
exits: {},
fn: doEverything
};