1

我正在研究风帆钩(项目钩)。我想让它可配置并且需要一种提供默认值的方法。

我找不到有关sails hooks 文档的信息。

这是一个示例钩子:

// api/hooks/customHook.js
module.exports = function customHook(sails) {
  return {
    configure: function () {
      console.log("Configure: ");
      console.log(sails.config[this.configKey]);
    },

    initialize: function (cb) {
      console.log("Initialize: ");
      console.log(sails.config[this.configKey]);
    },
  };
};

当我在没有创建config/customhook.js文件的情况下提升我的应用程序时,我得到以下输出:

配置:
未定义
初始化:
未定义


我需要一种为可配置值定义默认值的方法(我们将其命名为configureValue)。帆中有什么东西可以帮助我们还是我们必须“手动”做,如果我们有嵌套的配置键,这可能会很痛苦:

// api/hooks/customHook.js
module.exports = function customHook(sails) {
  return {
    configure: function () {
      if (typeof sails.config[this.configKey] == "undefined") {
        sails.config[this.configKey] = {};
      }

      if (typeof sails.config[this.configKey].configurableValue == "undefined") {
        sails.config[this.configKey].configurableValue = "defaultValue";
      }
    },

    initialize: function (cb) {
      console.log("Initialize: ");
      console.log(sails.config[this.configKey]);
    },
  };
};

PS:我知道有很多 NPM 模块可以帮助我,包括 lodash,但我正在寻找一种风帆推荐的方式来做到这一点。

4

0 回答 0