Skip to content

Commit

Permalink
Fix loopback.getCurrentContext
Browse files Browse the repository at this point in the history
 - ensure the method is always defined

 - return `null` when the context is not active
   (we are not inside a request-handling chain)
  • Loading branch information
Miroslav Bajtoš committed Nov 11, 2014
1 parent c29ace3 commit 8f5aea3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 8f5aea3

Please sign in to comment.