Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ app.model = function(Model, config) {
Model = registry.createModel(modelConfig);

// delete config options already applied
['relations', 'base', 'acls', 'hidden'].forEach(function(prop) {
['relations', 'base', 'acls', 'hidden', 'methods'].forEach(function(prop) {
delete config[prop];
if (config.options) delete config.options[prop];
});
Expand Down
26 changes: 26 additions & 0 deletions lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ Registry.prototype.createModel = function(name, properties, options) {
var model = BaseModel.extend(name, properties, options);
model.registry = this;

this._defineRemoteMethods(model, options.methods);

// try to attach
try {
this.autoAttachModel(model);
Expand Down Expand Up @@ -247,6 +249,30 @@ Registry.prototype.configureModel = function(ModelCtor, config) {
'Use `null` or `false` to mark models not attached to any data source.',
modelName);
}

// Remote methods
this._defineRemoteMethods(ModelCtor, config.methods);
};

Registry.prototype._defineRemoteMethods = function(ModelCtor, methods) {
if (!methods) return;
if (typeof methods !== 'object') {
console.warn('Ignoring non-object "methods" setting of "%s".',
ModelCtor.modelName);
return;
}

Object.keys(methods).forEach(function(key) {
var meta = methods[key];
if (typeof meta.isStatic !== 'boolean') {
console.warn('Remoting metadata for "%s.%s" is missing "isStatic" ' +
'flag, the method is registered as an instance method.',
ModelCtor.modelName,
key);
console.warn('This behaviour may change in the next major version.');

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to report a warning instead of throwing an exception, because what if somebody was already using the "methods" option and implemented a solution similar to this patch? Throwing an exception would break backwards-compatibility for that case.

However, if you @ritch think I am too cautious and it's ok to throw an error, then I'll rework this part.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you have is fine...

}
ModelCtor.remoteMethod(key, meta);
});
};

/**
Expand Down
50 changes: 50 additions & 0 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ describe('loopback', function() {
.to.throw(Error, new RegExp('Model not found: ' + uniqueModelName));
});
});

it('configures remote methods', function() {
var TestModel = loopback.createModel(uniqueModelName, {}, {
methods: {
staticMethod: {
isStatic: true,
http: { path: '/static' }
},
instanceMethod: {
isStatic: false,
http: { path: '/instance' }
}
}
});

var methodNames = TestModel.sharedClass.methods().map(function(m) {
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
});

expect(methodNames).to.include.members([
'staticMethod',
'prototype.instanceMethod'
]);
});
});

describe('loopback.createModel(config)', function() {
Expand Down Expand Up @@ -449,6 +473,32 @@ describe('loopback', function() {
// configureModel MUST NOT change Model's base class
expect(model.settings.base.name).to.equal(baseName);
});

it('configures remote methods', function() {
var TestModel = loopback.createModel(uniqueModelName);
loopback.configureModel(TestModel, {
dataSource: null,
methods: {
staticMethod: {
isStatic: true,
http: { path: '/static' }
},
instanceMethod: {
isStatic: false,
http: { path: '/instance' }
}
}
});

var methodNames = TestModel.sharedClass.methods().map(function(m) {
return m.stringName.replace(/^[^.]+\./, ''); // drop the class name
});

expect(methodNames).to.include.members([
'staticMethod',
'prototype.instanceMethod'
]);
});
});

describe('loopback object', function() {
Expand Down