Skip to content

Commit

Permalink
squash! de34d55
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bajtos committed Jan 2, 2017
1 parent de34d55 commit 23bb38d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -200,19 +187,34 @@ 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'}
];

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;
Expand Down
22 changes: 22 additions & 0 deletions test/context-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 23bb38d

Please sign in to comment.