From f0e3c913d8e6bcb7bb1263b8d906e98cb149be96 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Tue, 6 Dec 2016 17:02:40 -0500 Subject: [PATCH] work in progress multi-level nest-remoting --- lib/model.js | 45 +++++++++++++++++++++-------------- test/relations.integration.js | 32 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 18 deletions(-) diff --git a/lib/model.js b/lib/model.js index 465601b3..71b75c18 100644 --- a/lib/model.js +++ b/lib/model.js @@ -761,6 +761,29 @@ module.exports = function(registry) { } }; + function buildNestedRemoteMethodOpts(method) { + var opts = {}; + + opts.accepts = acceptArgs.concat(method.accepts || []); + opts.returns = [].concat(method.returns || []); + opts.description = method.description; + opts.accessType = method.accessType; + opts.rest = extend({}, method.rest || {}); + opts.rest.delegateTo = method; + + opts.http = []; + var routes = [].concat(method.http || []); + routes.forEach(function(route) { + if (route.path) { + var copy = extend({}, route); + copy.path = httpPath + route.path; + opts.http.push(copy); + } + }); + return opts; + } + + // build sharedToClass.methods().forEach(function(method) { var methodName; if (!method.isStatic && (methodName = filter(method, relation))) { @@ -777,27 +800,12 @@ module.exports = function(registry) { throw new Error(g.f('Invalid remote method: `%s`', method.name)); } - var opts = {}; - - opts.accepts = acceptArgs.concat(method.accepts || []); - opts.returns = [].concat(method.returns || []); - opts.description = method.description; - opts.accessType = method.accessType; - opts.rest = extend({}, method.rest || {}); - opts.rest.delegateTo = method; - - opts.http = []; - var routes = [].concat(method.http || []); - routes.forEach(function(route) { - if (route.path) { - var copy = extend({}, route); - copy.path = httpPath + route.path; - opts.http.push(copy); - } - }); + var opts = buildNestedRemoteMethodOpts(method); + // console.log('\n ------------------------- \n methodName: %s', methodName, opts); if (relation.multiple) { sharedClass.defineMethod(methodName, opts, function(fkId) { + console.log('methodName: %s', methodName); var args = Array.prototype.slice.call(arguments, 1); var last = args[args.length - 1]; var cb = typeof last === 'function' ? last : null; @@ -816,6 +824,7 @@ module.exports = function(registry) { }, method.isStatic); } else { sharedClass.defineMethod(methodName, opts, function() { + console.log('methodName: %s', methodName); var args = Array.prototype.slice.call(arguments); var last = args[args.length - 1]; var cb = typeof last === 'function' ? last : null; diff --git a/test/relations.integration.js b/test/relations.integration.js index c0004c6b..456ddf61 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -1468,6 +1468,12 @@ describe('relations - integration', function() { ); app.model(Note, {dataSource: 'db'}); + var subNote = app.registry.createModel( + 'SubNote', + {text: 'string'} + ); + app.model(subNote, {dataSource: 'db'}); + var Chapter = app.registry.createModel( 'Chapter', {name: 'string'}, @@ -1478,6 +1484,7 @@ describe('relations - integration', function() { Book.hasMany(Page); Book.hasMany(Chapter); Page.hasMany(Note); + Note.hasMany(subNote); Chapter.hasMany(Note); Image.belongsTo(Book); @@ -1488,7 +1495,9 @@ describe('relations - integration', function() { Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'}}); + Page.nestRemoting('notes'); Book.nestRemoting('pages'); + // Book.nestRemoting('notes'); Book.nestRemoting('chapters'); Image.nestRemoting('book'); @@ -1586,6 +1595,29 @@ describe('relations - integration', function() { }); }); + it('it has nested relationship routes - notes', function(done) { + var app = this.app; + var bookMethodsStringName = app._remotes._classes.Book._methods + .map(function(item) { return item.stringName; }); + console.log(bookMethodsStringName); + expect(bookMethodsStringName).to.contain('Book.prototype.__get__pages__notes'); + done(); + }); + + it('it has nested relationship routes', function(done) { + var app = this.app; + var pageMethodsStringName = app._remotes._classes.Page._methods + .map(function(item) { return item.stringName; }); + var bookMethodsStringName = app._remotes._classes.Book._methods + .map(function(item) { return item.stringName; }); + console.log(pageMethodsStringName); + expect(pageMethodsStringName).to.contain('Page.prototype.__get__notes__subNotes'); + expect(bookMethodsStringName).to.contain('Book.prototype.__get__pages__notes'); + expect(bookMethodsStringName).to.contain('Book.prototype.__get__pages__notes__subNotes'); + done(); + }); + + it('has a basic error handler', function(done) { var test = this; this.get('/api/books/unknown/pages/' + test.page.id + '/notes')