Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 600 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create config variables in sails.js?

#1
I'm converting an app of mine from Express to [sails.js](

[To see links please register here]

) - 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.
Reply

#2
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)



Reply

#3
I just made a service which delivers a value:

maxLimbs: function(){
var maxLimbs = 15;
return maxLimbs;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through