From 0faff121bee982f9574f912aa2c6c921324f8b12 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Wed, 4 Mar 2015 16:48:22 +0100 Subject: [PATCH 1/2] Define remote methods using json schema definitions. Prototype for #209 Signed-off-by: Tom Kirkpatrick --- lib/application.js | 2 +- lib/registry.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/application.js b/lib/application.js index 64f5ea681..14e0bbb1f 100644 --- a/lib/application.js +++ b/lib/application.js @@ -119,7 +119,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', 'methods', 'hidden'].forEach(function(prop) { delete config[prop]; if (config.options) delete config.options[prop]; }); diff --git a/lib/registry.js b/lib/registry.js index cf39879ad..adc0b4bc3 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -201,12 +201,24 @@ registry.configureModel = function(ModelCtor, config) { 'must be an array of objects', modelName); } + // Remote methods + if (typeof settings.methods === 'object' && settings.methods !== null) { + var methods = settings.methods = settings.methods || {}; + Object.keys(settings.methods).forEach(function(key) { + ModelCtor.remoteMethod(key, settings.methods[key]); + }); + } else if (settings.methods != null) { + console.warn('The methods property of `%s` configuration ' + + 'must be an object', modelName); + } + // Settings var excludedProperties = { base: true, 'super': true, relations: true, acls: true, + methods: true, dataSource: true }; if (typeof config.options === 'object' && config.options !== null) { From caa282838eb6170df6592d37b8e1351d53960f2b Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Thu, 12 Mar 2015 12:23:30 +0100 Subject: [PATCH 2/2] Add basic test case to proove that remote methods defined via json are added to the model #209 Signed-off-by: Tom Kirkpatrick --- .../simple-integration-app/models.json | 34 +++++++++++++++++++ test/remoting.integration.js | 4 ++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/test/fixtures/simple-integration-app/models.json b/test/fixtures/simple-integration-app/models.json index af1cd5904..c3043f351 100644 --- a/test/fixtures/simple-integration-app/models.json +++ b/test/fixtures/simple-integration-app/models.json @@ -57,6 +57,40 @@ "model": "widget", "type": "hasMany" } + }, + "methods": { + "details": { + "description": "Get additional details about a store.", + "accepts": [{ + "arg": "id", + "type": "any", + "required": true, + "description": "PersistedModel Id" + }], + "returns": { + "arg": "store", + "type": "store", + "root": true + }, + "http": { + "path": "/:id/details", + "verb": "get" + } + }, + "test": { + "description": "Get additional information about a store.", + "isStatic": false, + "accepts": [], + "returns": { + "arg": "store", + "type": "store", + "root": true + }, + "http": { + "path": "/test", + "verb": "get" + } + } } } }, diff --git a/test/remoting.integration.js b/test/remoting.integration.js index 76840f69a..de9047eb3 100644 --- a/test/remoting.integration.js +++ b/test/remoting.integration.js @@ -120,7 +120,9 @@ describe('remoting - integration', function() { 'updateAll(where:object,data:object) POST /stores/update', 'deleteById(id:any) DELETE /stores/:id', 'count(where:object):number GET /stores/count', - 'prototype.updateAttributes(data:object):store PUT /stores/:id' + 'prototype.updateAttributes(data:object):store PUT /stores/:id', + 'details(id:any):store GET /stores/:id/details', + 'prototype.test():store GET /stores/:id/test' ]; // The list of methods is from docs: