如何在控制器外的sailsjs中访问我的模型? [英] How can I access my models in sailsjs outside a controller?
问题描述
我有一个看起来像这样的控制器.
I have a controller that looks like this.
module.exports = {
readbyslug: function (req, res) {
var slug = req.param('id');
Store.findOneByName(slug.split('-').join(' '))
.then(function(err, store){
if(err)
res.send({status:'error', msg:'no store'});
res.send(store);
});
}
};
但是,我不想在控制器中拥有所有这些登录信息,我宁愿将它们粘贴在另一个模块中.
however, I would rather not have all this login in the controllers, I would rather stick them in another module.
我的问题是,如何访问模型?他们是全球性的还是什么?
My question is, how do I access the models? are they global or something?
推荐答案
Chadams——默认情况下模型是全局的.所以如果你做了sails generate model foo
,你可以在你的代码中访问Foo
.如果您不想以这种方式访问它们,您可以使用 sails
引用来访问 sails.models.foo
,这是同样的事情.
Chadams-- models are global by default. So if you did sails generate model foo
, you could access Foo
in your code. If you'd rather not access them that way, you can use the sails
reference to access sails.models.foo
, which is the same thing.
希望有帮助!
顺便说一句,此自动全球化可通过自定义 sails 来禁用.config.globals
.例如:
btw this auto-globalization is disable-able by customizing
sails.config.globals
. For example:
// To configure what's available globally, make a new file, `config/globals.js`
// with the following contents:
// In this example, I'll disable globalization of models, as well as _ and async.
// (But I'll leave the `sails` global available.)
// Variables which will be made globally accessible to the server process
module.exports.globals = {
_ : false,
async: false,
models: false,
sails: true
};
这篇关于如何在控制器外的sailsjs中访问我的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!