Merge pull request #299 from strongloop/feature/implement-app-settings-in-browser

Feature/implement app settings in browser
This commit is contained in:
Miroslav Bajtoš 2014-06-03 21:33:14 +02:00
commit 93a74f2821
8 changed files with 95 additions and 22 deletions

View File

@ -66,7 +66,8 @@ module.exports = function(grunt) {
files: [
'test/support.js',
'test/model.test.js',
'test/geo-point.test.js'
'test/geo-point.test.js',
'test/app.test.js'
],
// list of files to exclude

View File

@ -1,7 +1,25 @@
module.exports = browserExpress;
function browserExpress() {
return {};
return new BrowserExpress();
}
browserExpress.errorHandler = {};
function BrowserExpress() {
this.settings = {};
}
BrowserExpress.prototype.set = function(key, value) {
if (arguments.length == 1) {
return this.get(key);
}
this.settings[key] = value;
return this; // fluent API
};
BrowserExpress.prototype.get = function(key) {
return this.settings[key];
};

View File

@ -1,7 +1,7 @@
var express = require('express');
var path = require('path');
var middlewares = express.middlewares = {};
var middlewares = exports;
function safeRequire(m) {
try {
@ -50,7 +50,3 @@ middlewares.favicon = function (icon, options) {
icon = icon || path.join(__dirname, '../favicon.ico');
return favicon(icon, options);
};
module.exports = express;

View File

@ -2,7 +2,7 @@
* Module dependencies.
*/
var express = require('./express-middleware')
var express = require('express')
, fs = require('fs')
, ejs = require('ejs')
, path = require('path')
@ -89,8 +89,7 @@ function createApplication() {
}
/*!
* Expose express.middleware as loopback.*
* for example `loopback.errorHandler` etc.
* Expose static express methods like `express.errorHandler`.
*/
for (var key in express) {
@ -100,11 +99,22 @@ for (var key in express) {
, Object.getOwnPropertyDescriptor(express, key));
}
for (var key in express.middlewares) {
Object.defineProperty(
loopback
, key
, Object.getOwnPropertyDescriptor(express.middlewares, key));
/*!
* Expose additional middleware like session as loopback.*
* This will keep the loopback API compatible with express 3.x
*
* ***only in node***
*/
if (loopback.isServer) {
var middlewares = require('./express-middleware');
for (var key in middlewares) {
Object.defineProperty(
loopback
, key
, Object.getOwnPropertyDescriptor(middlewares, key));
}
}
/*!

View File

@ -34,7 +34,7 @@
"debug": "~0.8.1",
"express": "4.x",
"body-parser": "~1.2.2",
"strong-remoting": "2.0.0-beta2",
"strong-remoting": "2.0.0-beta3",
"inflection": "~1.3.5",
"passport": "~0.2.0",
"passport-local": "~1.0.0",

View File

@ -3,6 +3,9 @@ var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app');
var loopback = require('../');
var DataModel = loopback.DataModel;
var describe = require('./util/describe');
var it = require('./util/it');
describe('app', function() {
describe('app.model(Model)', function() {
@ -35,7 +38,7 @@ describe('app', function() {
expect(classes).to.contain('color');
});
it('updates REST API when a new model is added', function(done) {
it.onServer('updates REST API when a new model is added', function(done) {
app.use(loopback.rest());
request(app).get('/colors').expect(404, function(err, res) {
if (err) return done(err);
@ -76,6 +79,8 @@ describe('app', function() {
app = loopback();
app.boot({
app: {port: 3000, host: '127.0.0.1'},
// prevent loading of models.json, it is not available in the browser
models: {},
dataSources: {
db: {
connector: 'memory'
@ -144,7 +149,7 @@ describe('app', function() {
});
});
describe('app.boot([options])', function () {
describe.onServer('app.boot([options])', function () {
beforeEach(function () {
app.boot({
app: {
@ -313,7 +318,7 @@ describe('app', function() {
});
});
describe('app.boot(appRootDir)', function () {
describe.onServer('app.boot(appRootDir)', function () {
it('Load config files', function () {
var app = loopback();
@ -327,7 +332,7 @@ describe('app', function() {
});
});
describe('listen()', function() {
describe.onServer('listen()', function() {
it('starts http server', function(done) {
var app = loopback();
app.set('port', 0);
@ -387,7 +392,7 @@ describe('app', function() {
});
});
describe('enableAuth', function() {
describe.onServer('enableAuth', function() {
it('should set app.isAuthEnabled to true', function() {
expect(app.isAuthEnabled).to.not.equal(true);
app.enableAuth();
@ -395,7 +400,7 @@ describe('app', function() {
});
});
describe('app.get("/", loopback.status())', function () {
describe.onServer('app.get("/", loopback.status())', function () {
it('should return the status of the application', function (done) {
var app = loopback();
app.get('/', loopback.status());
@ -456,4 +461,26 @@ describe('app', function() {
expect(app.connectors.FOOBAR).to.equal(loopback.Memory);
});
});
describe('app.settings', function() {
it('can be altered via `app.set(key, value)`', function() {
app.set('write-key', 'write-value');
expect(app.settings).to.have.property('write-key', 'write-value');
});
it('can be read via `app.get(key)`', function() {
app.settings['read-key'] = 'read-value';
expect(app.get('read-key')).to.equal('read-value');
});
it('is unique per app instance', function() {
var app1 = loopback();
var app2 = loopback();
expect(app1.settings).to.not.equal(app2.settings);
app1.set('key', 'value');
expect(app2.get('key'), 'app2 value').to.equal(undefined);
});
});
});

View File

@ -5,6 +5,8 @@ var Change = loopback.Change;
var defineModelTestsWithDataSource = require('./util/model-tests');
var DataModel = loopback.DataModel;
var describe = require('./util/describe');
describe('Model / DataModel', function() {
defineModelTestsWithDataSource({
dataSource: {

19
test/util/it.js Normal file
View File

@ -0,0 +1,19 @@
var loopback = require('../../');
module.exports = it;
it.onServer = function itOnServer(name, fn) {
if (loopback.isServer) {
it(name, fn);
} else {
it.skip(name, fn);
}
};
it.inBrowser = function itInBrowser(name, fn) {
if (loopback.isBrowser) {
it(name, fn);
} else {
it.skip(name, fn);
}
};