Fix `loopback.getCurrentContext`

- ensure the method is always defined

 - return `null` when the context is not active
   (we are not inside a request-handling chain)
This commit is contained in:
Miroslav Bajtoš 2014-11-10 19:12:00 +01:00
parent c29ace3404
commit 8f5aea3e3b
3 changed files with 10 additions and 1 deletions

View File

@ -185,6 +185,11 @@ loopback.template = function(file) {
return ejs.compile(str);
};
loopback.getCurrentContext = function() {
// A placeholder method, see lib/middleware/context.js for the real version
return null;
};
/*!
* Built in models / services
*/

View File

@ -16,7 +16,7 @@ function createContext(scope) {
process.context[scope] = ns;
// Set up loopback.getCurrentContext()
loopback.getCurrentContext = function() {
return ns;
return ns && ns.active ? ns : null;
};
chain(juggler);

View File

@ -20,6 +20,10 @@ describe('loopback', function() {
expect(require('fs').existsSync(loopback.faviconFile), 'file exists')
.to.equal(true);
});
it.onServer('has `getCurrentContext` method', function() {
expect(loopback.getCurrentContext).to.be.a('function');
});
});
describe('loopback.createDataSource(options)', function() {