Merge pull request #962 from clarkorz/fix/nestRemote-hooks

fix nestRemoting is nesting hooks from other relations
This commit is contained in:
Raymond Feng 2015-01-06 15:29:15 -08:00
commit d77c5fac1d
2 changed files with 37 additions and 6 deletions

View File

@ -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);
});
}
}

View File

@ -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;
test.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) {