From d6fc3cba1b6344f54d7284b71145457f6ae440fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 2 Jan 2017 16:08:25 +0100 Subject: [PATCH] squash! de34d55 Fix sharedCtor remoting metadata to honour the new flag injectOptionsFromRemoteContext. Fix construction of sharedCtor remoting metadata to prevent the situation when we are configuring remoting metadata after strong-remoting has already picked up data from our parent (base) model. --- lib/model.js | 37 +++++++++++++++++++----------------- test/context-options.test.js | 22 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/model.js b/lib/model.js index 50e91cdfe..09233b185 100644 --- a/lib/model.js +++ b/lib/model.js @@ -126,22 +126,9 @@ module.exports = function(registry) { 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, - remotingOptions - ); - - // setup a remoting type converter for this model - RemoteObjects.convert(typeName, function(val) { - return val ? new ModelCtor(val) : val; - }); - // support remoting prototype methods + // it's important to setup this function *before* calling `new SharedClass` + // otherwise remoting metadata from our base model is picked up ModelCtor.sharedCtor = function(data, id, options, fn) { var ModelCtor = this; @@ -200,12 +187,12 @@ module.exports = function(registry) { }; var idDesc = ModelCtor.modelName + ' id'; - ModelCtor.sharedCtor.accepts = [ + ModelCtor.sharedCtor.accepts = this._removeOptionsArgIfDisabled([ {arg: 'id', type: 'any', required: true, http: {source: 'path'}, description: idDesc}, // {arg: 'instance', type: 'object', http: {source: 'body'}} {arg: 'options', type: 'object', http: createOptionsViaModelMethod}, - ]; + ]); ModelCtor.sharedCtor.http = [ {path: '/:id'} @@ -213,6 +200,22 @@ module.exports = function(registry) { ModelCtor.sharedCtor.returns = {root: true}; + + var remotingOptions = {}; + extend(remotingOptions, options.remoting || {}); + + // create a sharedClass + var sharedClass = ModelCtor.sharedClass = new SharedClass( + ModelCtor.modelName, + ModelCtor, + remotingOptions + ); + + // setup a remoting type converter for this model + RemoteObjects.convert(typeName, function(val) { + return val ? new ModelCtor(val) : val; + }); + // before remote hook ModelCtor.beforeRemote = function(name, fn) { var className = this.modelName; diff --git a/test/context-options.test.js b/test/context-options.test.js index c3f629c12..ac8f50412 100644 --- a/test/context-options.test.js +++ b/test/context-options.test.js @@ -128,6 +128,28 @@ describe('OptionsFromRemotingContext', function() { }); }); + it('honours injectOptionsFromRemoteContext in sharedCtor', function() { + var settings = { + forceId: false, + injectOptionsFromRemoteContext: false, + }; + var TestModel = app.registry.createModel('TestModel', {}, settings); + app.model(TestModel, {dataSource: 'db'}); + + TestModel.prototype.dummy = function(cb) { cb(); }; + TestModel.remoteMethod('dummy', {isStatic: false}); + + observeOptionsOnAccess(TestModel); + + return TestModel.create({id: 1}) + .then(function() { + return request.post('/TestModels/1/dummy').expect(204); + }) + .then(function() { + expect(actualOptions).to.eql({}); + }); + }); + // Catch: because relations methods are defined on "modelFrom", // they will invoke createOptionsFromRemotingContext on "modelFrom" too, // despite the fact that under the hood a method on "modelTo" is called.