gpt4 book ai didi

sails.js - 从模块加载器sails.js 中排除文件如何在内部使用

转载 作者:行者123 更新时间:2023-12-01 00:49:05 24 4
gpt4 key购买 nike

我将sails.js 与我创建的名为def-inc 的模块一起使用通过 mixins 对 Controller 和模型进行某种继承。现在我想将混合/特征存储在模型和 Controller 内的特征文件夹中。我不想用另一个文件夹污染 api 根来保存我的特征,所以要点是,如果可以排除文件夹或文件,而不必修改核心文件? ,或者至少是一种覆盖模块加载器并将其配置为执行此操作的方法。

这是我想要使用的路径结构的一个示例,但没有获得额外的模型/ Controller 。

.                                         
|-- api
| |-- models
| | |-- traits
| | | |-- accountTraits.js
| | |-- User.coffee
| |-- controllers
| | |-- traits
| | | |-- restfullTraits.js
| | |-- UserController.js

现在,如果我这样做,我会得到一个名为 accountTraits 的额外模型(如果使用 mysql 适配器,还有一个表)。

我已经检查了代码和文档,到目前为止这似乎不支持 atm,但由于使用模型中的其他对象可能是一种常规模式(外部风 sails 、轨道、laravel 等)域,但不是特定的数据库模型,我假设有人做过类似的事情。

注意:我知道为简单起见,我可以将特征文件夹移动到 api 根路径,并且我不认为特征是服务的一部分,所以请避免回答,如果不可能,请评论我的问题.

编辑:
基于 提供的代码@sgress454 ,我创建了这段代码,只是为了支持 loadModels(它的工作方式相同),并且有一个 fn 可以修改,以防我想将相同的行为应用于其他 moduleLoader 方法。无论如何,我会把它留在这里以防万一有人需要它(但是 一定要支持@sgress454 :)
var liftOptions = rc('sails');
// Create the module loader override function
liftOptions.moduleLoaderOverride = function(sails, base) {
// Get a reference to the base loaders methods we want to extend
var baseLoadController = base.loadControllers;
var baseLoadModels = base.loadModels;

// Reusable fn to remove modules that match the defined pattern
var removeTraitsFromAutoLoadModules = function(cb, err, modules){
// Remove all modules whose identity ends with "traits"
modules = _.omit(modules, function(module, identity) {
return identity.match(/traits$/);
});
// Return the rest
return cb(err, modules);
};

return {
loadControllers: function (cb) {
baseLoadController(removeTraitsFromAutoLoadModules.bind(null, cb));
},
loadModels: function(cb) {
baseLoadModels(removeTraitsFromAutoLoadModules.bind(null, cb));
}
};
};


// Start server
sails.lift(liftOptions);

最佳答案

您可以通过传递 moduleLoaderOverride 来覆盖模块加载器作为 sails.lift 的一个选项.该函数有两个参数——一个对 Sails 实例的引用,以及一个包含原始模块加载器方法的对象,以便您仍然可以调用它们。该函数应返回一个对象,其中包含您想要覆盖的模块加载器的方法。例如:

// bottom of app.js

// Get the lift options from the .sailsrc file
var liftOptions = rc('sails');

// Include lodash (you may have to npm install it), or else rewrite
// below without the _.omit call
var _ = require('lodash');

// Create the module loader override function
liftOptions.moduleLoaderOverride = function(sails, base) {
// Get a reference to the base loadControllers method we want to extend
var baseLoadControllers = base.loadControllers;
return {
loadControllers: function (cb) {
// Load all of the controllers
baseLoadControllers(function(err, controllers) {
// Remove all controllers whose identity starts with "traits"
controllers = _.omit(controllers, function(controller, identity) {return identity.match(/^traits/);});
// Return the rest
return cb(err, controllers);
});
}
};
};

// Lift Sails
sails.lift(liftOptions);

您必须使用 node app.js 提升您的应用程序要让它工作——没有办法把它放在一个常规的配置文件中并使用 sails lift ,因为这些是由模块加载器加载的!

关于sails.js - 从模块加载器sails.js 中排除文件如何在内部使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32431025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com