From 94b2a45a6c9672bbb3fa7c2b80bd41c5f7db6b2b Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Thu, 1 Jan 2015 15:26:58 +0800 Subject: [PATCH 1/2] fix nestRemoting is nesting hooks from other relations Signed-off-by: Clark Wang --- lib/model.js | 12 ++++++------ test/relations.integration.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/model.js b/lib/model.js index d93e4ab4..3ea73d9b 100644 --- a/lib/model.js +++ b/lib/model.js @@ -654,7 +654,7 @@ Model.nestRemoting = function(relationName, options, cb) { opts.returns = [].concat(method.returns || []); opts.description = method.description; opts.rest = extend({}, method.rest || {}); - opts.rest.delegateTo = method.name; + opts.rest.delegateTo = method; opts.http = []; var routes = [].concat(method.http || []); @@ -718,18 +718,18 @@ Model.nestRemoting = function(relationName, options, cb) { sharedClass.methods().forEach(function(method) { var delegateTo = method.rest && method.rest.delegateTo; - if (delegateTo) { + if (delegateTo && delegateTo.ctor == relation.modelTo) { var before = method.isStatic ? beforeListeners : beforeListeners['prototype']; var after = method.isStatic ? afterListeners : afterListeners['prototype']; var m = method.isStatic ? method.name : 'prototype.' + method.name; - if (before && before[delegateTo]) { + if (before && before[delegateTo.name]) { self.beforeRemote(m, function(ctx, result, next) { - before[delegateTo]._listeners.call(null, ctx, next); + before[delegateTo.name]._listeners.call(null, ctx, next); }); } - if (after && after[delegateTo]) { + if (after && after[delegateTo.name]) { self.afterRemote(m, function(ctx, result, next) { - after[delegateTo]._listeners.call(null, ctx, next); + after[delegateTo.name]._listeners.call(null, ctx, next); }); } } diff --git a/test/relations.integration.js b/test/relations.integration.js index f1aa72da..59e77df2 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -1164,8 +1164,15 @@ describe('relations - integration', function() { { properties: { text: 'string' }, dataSource: 'db', plural: 'notes' } ); + var Chapter = app.model( + 'Chapter', + { properties: { name: 'string' }, dataSource: 'db', + plural: 'chapters' } + ); Book.hasMany(Page); + Book.hasMany(Chapter); Page.hasMany(Note); + Chapter.hasMany(Note); Image.belongsTo(Book); // fake a remote method that match the filter in Model.nestRemoting() @@ -1176,6 +1183,7 @@ describe('relations - integration', function() { Page.remoteMethod('__throw__errors', { isStatic: false, http: { path: '/throws', verb: 'get' } }); Book.nestRemoting('pages'); + Book.nestRemoting('chapters'); Image.nestRemoting('book'); expect(Book.prototype['__findById__pages__notes']).to.be.a.function; @@ -1212,6 +1220,19 @@ describe('relations - integration', function() { }); }); + before(function createChapters(done) { + var test = this, book = test.book; + book.chapters.create({ name: 'Chapter 1' }, + function(err, chapter) { + if (err) return done(err); + test.chapter = chapter; + chapter.notes.create({ text: 'Chapter Note 1' }, function(err, note) { + test.cnote = note; + done(); + }); + }); + }); + before(function createCover(done) { var test = this; app.models.Image.create({ name: 'Cover 1', book: test.book }, @@ -1300,6 +1321,16 @@ describe('relations - integration', function() { }); }); + it('should nest remote hooks of ModelTo - hasMany findById', function(done) { + var test = this; + this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id) + .expect(200, function(err, res) { + expect(res.headers['x-before']).to.empty(); + expect(res.headers['x-after']).to.empty(); + done(); + }); + }); + it('should have proper http.path for remoting', function() { [app.models.Book, app.models.Image].forEach(function(Model) { Model.sharedClass.methods().forEach(function(method) { From 58f67e92d157ba4fb23ee1ada3541e376858e859 Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Sun, 4 Jan 2015 18:24:29 +0800 Subject: [PATCH 2/2] fix jscs warning Signed-off-by: Clark Wang --- test/relations.integration.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/relations.integration.js b/test/relations.integration.js index 59e77df2..edd90064 100644 --- a/test/relations.integration.js +++ b/test/relations.integration.js @@ -1221,8 +1221,8 @@ describe('relations - integration', function() { }); before(function createChapters(done) { - var test = this, book = test.book; - book.chapters.create({ name: 'Chapter 1' }, + var test = this; + test.book.chapters.create({ name: 'Chapter 1' }, function(err, chapter) { if (err) return done(err); test.chapter = chapter;