简体   繁体   English

使用Sails.js中的钩子生成的数据

[英]Using data generated from a hook in sails.js

In sails.js, can a service use data or functions generated by a hook or by sails.config.bootstrap ? 在sails.js中,服务可以使用钩子或sails.config.bootstrap生成的数据或函数吗? Or it's only the other way around? 还是只有相反的方式?

EDIT: I was trying to add a hook to set up my rate limiter's parameters before sails lift , and then use this rate limiter from within the application. 编辑:我试图添加一个钩子以在sails lift之前设置我的速率限制器的参数,然后从应用程序内部使用此速率限制器。

The bootstrap file in config/bootstrap.js is intended mainly for setting up data, jobs, etc. that are specific to your app and may need to run only once. config/bootstrap.js的引导文件主要用于设置特定于您的应用程序的数据,作业等,并且可能只需要运行一次。 It's run after all of your models, services and hooks have already loaded, so it can rely on them. 它在所有模型,服务和挂钩都已加载后运行,因此可以依靠它们。

You can use a hook method inside of a service method--you just can't use it to set up a service. 您可以在服务方法内部使用钩子方法-只是无法使用它来设置服务。 So, this is okay: 所以,这没关系:

// config/services/GoodService.js
module.exports = {
  someMethod: function() {
    var rateLimit = sails.hooks.someHook.getRateLimit();        
  }
};

This is not okay: 这不行:

// config/services/BadService.js
var rateLimit = sails.hooks.someHook.getRateLimit();
module.exports = {
  someMethod: function() {...do something with rateLimit...}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM