在sails.js中创建配置变量? [英] Create config variables in sails.js?

查看:104
本文介绍了在sails.js中创建配置变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的一个应用程序从Express转换为 sails.js - 有没有办法在Sails中做这样的事情? / p>

从我的 app.js 文件在Express:

  var globals = {
name:'projectName',
author:'authorName'
};

app.get('/',function(req,res){
globals.page_title ='Home';
res.render('index',全局变量);
});

这让我可以在每个视图上访问这些变量,而无需将它们硬编码到模板中。

解决方案

您可以在 config / 文件夹。例如 config / myconf.js 与您的配置变量:

 模块.exports.myconf = {
name:'projectName',
author:'authorName',

anyobject:{
bar:foo
}
};

然后从任何视图通过全局 sails 变量



在视图中:



  ! -  views / foo / bar.ejs  - > 
<%= sails.config.myconf.name%>
<%= sails.config.myconf.author%>



服务



  // api / services / FooService.js 
模块。 exports = {

/ **
*一些功能,做的东西。
*
* @param {[type]} options [description]
* @param {Function} cb [description]
* /
lookupDumbledore:function ,cb){

//`sails`对象在这里可用:
var conf = sails.config;
cb(null,conf.whatever);
}
};

//`sails`在这里不可用
//(它还不存在)
console.log(sails); // ==>未定义



在一个模型中:



  // api / models / Foo.js 
module.exports = {
属性:{
// ...
},

someModelMethod:function(options,cb){

//`sails`对象在这里可用:
var conf = sails.config;
cb(null,conf.whatever);
}
};

//`sails在这里不可用
//(还不存在)



在控制器中:




注意:这在策略中的工作方式相同。




  // api / controllers / FooController.js 
module.exports = {
index :function(req,res){

//`sails`在这里可用

return res.json({
name:sails.config.myconf .name
});
}
};

//`sails在这里不可用
//(还不存在)


I'm converting an app of mine from Express to sails.js - is there a way I can do something like this in Sails?

From my app.js file in Express:

var globals = {
    name: 'projectName',
    author: 'authorName'
};

app.get('/', function (req, res) {
    globals.page_title = 'Home';
    res.render('index', globals);
});

This let me access those variables on every view without having to hardcode them into the template. Not sure how/where to do it in Sails though.

解决方案

You can create your own config file in config/ folder. For example config/myconf.js with your config variables:

module.exports.myconf = {
    name: 'projectName',
    author: 'authorName',

    anyobject: {
      bar: "foo"
    }
};

and then access these variables from any view via global sails variable.

In a view:

<!-- views/foo/bar.ejs -->
<%= sails.config.myconf.name %>
<%= sails.config.myconf.author %>

In a service

// api/services/FooService.js
module.exports = {

  /**
   * Some function that does stuff.
   *
   * @param  {[type]}   options [description]
   * @param  {Function} cb      [description]
   */
  lookupDumbledore: function(options, cb) {

    // `sails` object is available here:
    var conf = sails.config;
    cb(null, conf.whatever);
  }
};

// `sails` is not available out here
// (it doesn't exist yet)
console.log(sails);  // ==> undefined

In a model:

// api/models/Foo.js
module.exports = {
  attributes: {
    // ...
  },

  someModelMethod: function (options, cb) {

    // `sails` object is available here:
    var conf = sails.config;
    cb(null, conf.whatever);
  }
};

// `sails is not available out here
// (doesn't exist yet)

In a controller:

Note: This works the same way in policies.

// api/controllers/FooController.js
module.exports = {
  index: function (req, res) {

    // `sails` is available in here

    return res.json({
      name: sails.config.myconf.name
    });
  }
};

// `sails is not available out here
// (doesn't exist yet)

这篇关于在sails.js中创建配置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆