I'm building out some node.js modules and I have some libraries I'd like to push into this object
in this senario, I have
app.js
var api = require('./scripts/api.js');
var oauth = require('./scripts/oauth.js');
var db = require('./scripts/db.js')
var libraries = {
api : api,
db : db,
oauth : oauth,
}
var modules = require('./scripts/module.js');
modules.init(app, libraries);
module.js
module.exports = {
init : function(app,libraries) {
for (key in libraries) {
if (libraries.hasOwnProperty(key)) {
this[key] = libraries[key]
}
}
this.oauth.init(app,libraries);
}
}
api.js
module.exports = {
init : function(app, libraries) {
for (key in libraries) {
if (libraries.hasOwnProperty(key)) {
this[key] = libraries[key]
}
}
app.get('/api/linkedin/posts', function (req, res) {
//get the credentials
var userid = req.session.user_id;
var credentials = '';
getCredentials('linkedin',userid)
.then(function(result) {
this.db.store(credentials = JSON.parse(result))
})
});
},
}
and it works fine, What I'd like to happen however is instead of pushing it onto the module object itself. to push it onto the object scope so that I don't have to add this.library.function() to everything. In this way I can just call oauth.init() and access the library directly, is there any good way of doing this?
What I'd like to accomplish would be to have the same affect as if I did the following instead, I'm just trying to make these bootstraping methods magic
module.js
var api = {}
var oauth = {}
module.exports = {
init : function(app,libraries) {
api = libraries.api
oauth = libraries.oauth
oauth.init(app,libraries);
}
}
Here's a fiddle demonstrating the problem http://jsbin.com/cadejukijudu/1/edit