Skip to content

Commit

Permalink
Merge pull request #962 from clarkorz/fix/nestRemote-hooks
Browse files Browse the repository at this point in the history
fix nestRemoting is nesting hooks from other relations
  • Loading branch information
raymondfeng committed Jan 6, 2015
2 parents f3f1298 + 58f67e9 commit d77c5fa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []);
Expand Down Expand Up @@ -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);
});
}
}
Expand Down
31 changes: 31 additions & 0 deletions test/relations.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit d77c5fa

Please sign in to comment.