Skip to content

Commit 23bb38d

Browse files
committed
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.
1 parent de34d55 commit 23bb38d

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

lib/model.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,9 @@ module.exports = function(registry) {
126126
var options = this.settings;
127127
var typeName = this.modelName;
128128

129-
var remotingOptions = {};
130-
extend(remotingOptions, options.remoting || {});
131-
132-
// create a sharedClass
133-
var sharedClass = ModelCtor.sharedClass = new SharedClass(
134-
ModelCtor.modelName,
135-
ModelCtor,
136-
remotingOptions
137-
);
138-
139-
// setup a remoting type converter for this model
140-
RemoteObjects.convert(typeName, function(val) {
141-
return val ? new ModelCtor(val) : val;
142-
});
143-
144129
// support remoting prototype methods
130+
// it's important to setup this function *before* calling `new SharedClass`
131+
// otherwise remoting metadata from our base model is picked up
145132
ModelCtor.sharedCtor = function(data, id, options, fn) {
146133
var ModelCtor = this;
147134

@@ -200,19 +187,34 @@ module.exports = function(registry) {
200187
};
201188

202189
var idDesc = ModelCtor.modelName + ' id';
203-
ModelCtor.sharedCtor.accepts = [
190+
ModelCtor.sharedCtor.accepts = this._removeOptionsArgIfDisabled([
204191
{arg: 'id', type: 'any', required: true, http: {source: 'path'},
205192
description: idDesc},
206193
// {arg: 'instance', type: 'object', http: {source: 'body'}}
207194
{arg: 'options', type: 'object', http: createOptionsViaModelMethod},
208-
];
195+
]);
209196

210197
ModelCtor.sharedCtor.http = [
211198
{path: '/:id'}
212199
];
213200

214201
ModelCtor.sharedCtor.returns = {root: true};
215202

203+
var remotingOptions = {};
204+
extend(remotingOptions, options.remoting || {});
205+
206+
// create a sharedClass
207+
var sharedClass = ModelCtor.sharedClass = new SharedClass(
208+
ModelCtor.modelName,
209+
ModelCtor,
210+
remotingOptions
211+
);
212+
213+
// setup a remoting type converter for this model
214+
RemoteObjects.convert(typeName, function(val) {
215+
return val ? new ModelCtor(val) : val;
216+
});
217+
216218
// before remote hook
217219
ModelCtor.beforeRemote = function(name, fn) {
218220
var className = this.modelName;

test/context-options.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,28 @@ describe('OptionsFromRemotingContext', function() {
128128
});
129129
});
130130

131+
it('honours injectOptionsFromRemoteContext in sharedCtor', function() {
132+
var settings = {
133+
forceId: false,
134+
injectOptionsFromRemoteContext: false,
135+
};
136+
var TestModel = app.registry.createModel('TestModel', {}, settings);
137+
app.model(TestModel, {dataSource: 'db'});
138+
139+
TestModel.prototype.dummy = function(cb) { cb(); };
140+
TestModel.remoteMethod('dummy', {isStatic: false});
141+
142+
observeOptionsOnAccess(TestModel);
143+
144+
return TestModel.create({id: 1})
145+
.then(function() {
146+
return request.post('/TestModels/1/dummy').expect(204);
147+
})
148+
.then(function() {
149+
expect(actualOptions).to.eql({});
150+
});
151+
});
152+
131153
// Catch: because relations methods are defined on "modelFrom",
132154
// they will invoke createOptionsFromRemotingContext on "modelFrom" too,
133155
// despite the fact that under the hood a method on "modelTo" is called.

0 commit comments

Comments
 (0)