From 568c8662b451d22f4230b7920eb6e70544f17d64 Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Sat, 11 Oct 2014 16:36:29 +0200 Subject: [PATCH] Support per-model and per-handler remoting options Allow the developer to pass custom `remoting` options via Model settings, e.g. PersistedModel.extend( 'MyModel', { name: String }, { remoting: { normalizeHttpPath: true } }); Also add `options` arg to `app.handler`, this object is passed directly to strong-remoting handler. --- lib/application.js | 4 ++-- lib/model.js | 5 ++++- test/app.test.js | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/application.js b/lib/application.js index 8a5fb560..8c1fc2fd 100644 --- a/lib/application.js +++ b/lib/application.js @@ -263,14 +263,14 @@ app.remoteObjects = function () { * @triggers `mounted` events on shared class constructors (models) */ -app.handler = function (type) { +app.handler = function (type, options) { var handlers = this._handlers || (this._handlers = {}); if(handlers[type]) { return handlers[type]; } var remotes = this.remotes(); - var handler = this._handlers[type] = remotes.handler(type); + var handler = this._handlers[type] = remotes.handler(type, options); remotes.classes().forEach(function(sharedClass) { sharedClass.ctor.emit('mounted', app, sharedClass, remotes); diff --git a/lib/model.js b/lib/model.js index 7d89490d..d2b459ff 100644 --- a/lib/model.js +++ b/lib/model.js @@ -103,11 +103,14 @@ Model.setup = function () { var options = this.settings; var typeName = this.modelName; + var remotingOptions = {}; + extend(remotingOptions, options.remoting || {}); + // create a sharedClass var sharedClass = ModelCtor.sharedClass = new SharedClass( ModelCtor.modelName, ModelCtor, - options.remoting + remotingOptions ); // setup a remoting type converter for this model diff --git a/test/app.test.js b/test/app.test.js index f4d6dd28..80a307ba 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -351,4 +351,26 @@ describe('app', function() { var app = loopback(); expect(app.loopback).to.equal(loopback); }); + + describe('normalizeHttpPath option', function() { + var app, db; + beforeEach(function() { + app = loopback(); + db = loopback.createDataSource({ connector: loopback.Memory }); + }); + + it.onServer('normalizes the http path', function(done) { + var UserAccount = PersistedModel.extend( + 'UserAccount', + { name: String }, + { + remoting: { normalizeHttpPath: true } + }); + app.model(UserAccount); + UserAccount.attachTo(db); + + app.use(loopback.rest()); + request(app).get('/user-accounts').expect(200, done); + }); + }); });