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: [ files: [
'test/support.js', 'test/support.js',
'test/model.test.js', 'test/model.test.js',
'test/geo-point.test.js' 'test/geo-point.test.js',
'test/app.test.js'
], ],
// list of files to exclude // list of files to exclude

View File

@ -1,7 +1,25 @@
module.exports = browserExpress; module.exports = browserExpress;
function browserExpress() { function browserExpress() {
return {}; return new BrowserExpress();
} }
browserExpress.errorHandler = {}; 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 express = require('express');
var path = require('path'); var path = require('path');
var middlewares = express.middlewares = {}; var middlewares = exports;
function safeRequire(m) { function safeRequire(m) {
try { try {
@ -50,7 +50,3 @@ middlewares.favicon = function (icon, options) {
icon = icon || path.join(__dirname, '../favicon.ico'); icon = icon || path.join(__dirname, '../favicon.ico');
return favicon(icon, options); return favicon(icon, options);
}; };
module.exports = express;

View File

@ -2,7 +2,7 @@
* Module dependencies. * Module dependencies.
*/ */
var express = require('./express-middleware') var express = require('express')
, fs = require('fs') , fs = require('fs')
, ejs = require('ejs') , ejs = require('ejs')
, path = require('path') , path = require('path')
@ -89,8 +89,7 @@ function createApplication() {
} }
/*! /*!
* Expose express.middleware as loopback.* * Expose static express methods like `express.errorHandler`.
* for example `loopback.errorHandler` etc.
*/ */
for (var key in express) { for (var key in express) {
@ -100,11 +99,22 @@ for (var key in express) {
, Object.getOwnPropertyDescriptor(express, key)); , Object.getOwnPropertyDescriptor(express, key));
} }
for (var key in express.middlewares) { /*!
Object.defineProperty( * Expose additional middleware like session as loopback.*
loopback * This will keep the loopback API compatible with express 3.x
, key *
, Object.getOwnPropertyDescriptor(express.middlewares, key)); * ***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", "debug": "~0.8.1",
"express": "4.x", "express": "4.x",
"body-parser": "~1.2.2", "body-parser": "~1.2.2",
"strong-remoting": "2.0.0-beta2", "strong-remoting": "2.0.0-beta3",
"inflection": "~1.3.5", "inflection": "~1.3.5",
"passport": "~0.2.0", "passport": "~0.2.0",
"passport-local": "~1.0.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 loopback = require('../');
var DataModel = loopback.DataModel; var DataModel = loopback.DataModel;
var describe = require('./util/describe');
var it = require('./util/it');
describe('app', function() { describe('app', function() {
describe('app.model(Model)', function() { describe('app.model(Model)', function() {
@ -35,7 +38,7 @@ describe('app', function() {
expect(classes).to.contain('color'); 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()); app.use(loopback.rest());
request(app).get('/colors').expect(404, function(err, res) { request(app).get('/colors').expect(404, function(err, res) {
if (err) return done(err); if (err) return done(err);
@ -76,6 +79,8 @@ describe('app', function() {
app = loopback(); app = loopback();
app.boot({ app.boot({
app: {port: 3000, host: '127.0.0.1'}, app: {port: 3000, host: '127.0.0.1'},
// prevent loading of models.json, it is not available in the browser
models: {},
dataSources: { dataSources: {
db: { db: {
connector: 'memory' connector: 'memory'
@ -144,7 +149,7 @@ describe('app', function() {
}); });
}); });
describe('app.boot([options])', function () { describe.onServer('app.boot([options])', function () {
beforeEach(function () { beforeEach(function () {
app.boot({ app.boot({
app: { app: {
@ -313,7 +318,7 @@ describe('app', function() {
}); });
}); });
describe('app.boot(appRootDir)', function () { describe.onServer('app.boot(appRootDir)', function () {
it('Load config files', function () { it('Load config files', function () {
var app = loopback(); var app = loopback();
@ -327,7 +332,7 @@ describe('app', function() {
}); });
}); });
describe('listen()', function() { describe.onServer('listen()', function() {
it('starts http server', function(done) { it('starts http server', function(done) {
var app = loopback(); var app = loopback();
app.set('port', 0); 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() { it('should set app.isAuthEnabled to true', function() {
expect(app.isAuthEnabled).to.not.equal(true); expect(app.isAuthEnabled).to.not.equal(true);
app.enableAuth(); 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) { it('should return the status of the application', function (done) {
var app = loopback(); var app = loopback();
app.get('/', loopback.status()); app.get('/', loopback.status());
@ -456,4 +461,26 @@ describe('app', function() {
expect(app.connectors.FOOBAR).to.equal(loopback.Memory); 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 defineModelTestsWithDataSource = require('./util/model-tests');
var DataModel = loopback.DataModel; var DataModel = loopback.DataModel;
var describe = require('./util/describe');
describe('Model / DataModel', function() { describe('Model / DataModel', function() {
defineModelTestsWithDataSource({ defineModelTestsWithDataSource({
dataSource: { 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);
}
};