work in progress multi-level nest-remoting
This commit is contained in:
parent
a34f321d2b
commit
f0e3c913d8
41
lib/model.js
41
lib/model.js
|
@ -761,22 +761,7 @@ module.exports = function(registry) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sharedToClass.methods().forEach(function(method) {
|
function buildNestedRemoteMethodOpts(method) {
|
||||||
var methodName;
|
|
||||||
if (!method.isStatic && (methodName = filter(method, relation))) {
|
|
||||||
var prefix = relation.multiple ? '__findById__' : '__get__';
|
|
||||||
var getterName = options.getterName || (prefix + relationName);
|
|
||||||
|
|
||||||
var getterFn = relation.modelFrom.prototype[getterName];
|
|
||||||
if (typeof getterFn !== 'function') {
|
|
||||||
throw new Error(g.f('Invalid remote method: `%s`', getterName));
|
|
||||||
}
|
|
||||||
|
|
||||||
var nestedFn = relation.modelTo.prototype[method.name];
|
|
||||||
if (typeof nestedFn !== 'function') {
|
|
||||||
throw new Error(g.f('Invalid remote method: `%s`', method.name));
|
|
||||||
}
|
|
||||||
|
|
||||||
var opts = {};
|
var opts = {};
|
||||||
|
|
||||||
opts.accepts = acceptArgs.concat(method.accepts || []);
|
opts.accepts = acceptArgs.concat(method.accepts || []);
|
||||||
|
@ -795,9 +780,32 @@ module.exports = function(registry) {
|
||||||
opts.http.push(copy);
|
opts.http.push(copy);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return opts;
|
||||||
|
}
|
||||||
|
|
||||||
|
// build
|
||||||
|
sharedToClass.methods().forEach(function(method) {
|
||||||
|
var methodName;
|
||||||
|
if (!method.isStatic && (methodName = filter(method, relation))) {
|
||||||
|
var prefix = relation.multiple ? '__findById__' : '__get__';
|
||||||
|
var getterName = options.getterName || (prefix + relationName);
|
||||||
|
|
||||||
|
var getterFn = relation.modelFrom.prototype[getterName];
|
||||||
|
if (typeof getterFn !== 'function') {
|
||||||
|
throw new Error(g.f('Invalid remote method: `%s`', getterName));
|
||||||
|
}
|
||||||
|
|
||||||
|
var nestedFn = relation.modelTo.prototype[method.name];
|
||||||
|
if (typeof nestedFn !== 'function') {
|
||||||
|
throw new Error(g.f('Invalid remote method: `%s`', method.name));
|
||||||
|
}
|
||||||
|
|
||||||
|
var opts = buildNestedRemoteMethodOpts(method);
|
||||||
|
// console.log('\n ------------------------- \n methodName: %s', methodName, opts);
|
||||||
|
|
||||||
if (relation.multiple) {
|
if (relation.multiple) {
|
||||||
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
sharedClass.defineMethod(methodName, opts, function(fkId) {
|
||||||
|
console.log('methodName: %s', methodName);
|
||||||
var args = Array.prototype.slice.call(arguments, 1);
|
var args = Array.prototype.slice.call(arguments, 1);
|
||||||
var last = args[args.length - 1];
|
var last = args[args.length - 1];
|
||||||
var cb = typeof last === 'function' ? last : null;
|
var cb = typeof last === 'function' ? last : null;
|
||||||
|
@ -816,6 +824,7 @@ module.exports = function(registry) {
|
||||||
}, method.isStatic);
|
}, method.isStatic);
|
||||||
} else {
|
} else {
|
||||||
sharedClass.defineMethod(methodName, opts, function() {
|
sharedClass.defineMethod(methodName, opts, function() {
|
||||||
|
console.log('methodName: %s', methodName);
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
var last = args[args.length - 1];
|
var last = args[args.length - 1];
|
||||||
var cb = typeof last === 'function' ? last : null;
|
var cb = typeof last === 'function' ? last : null;
|
||||||
|
|
|
@ -1468,6 +1468,12 @@ describe('relations - integration', function() {
|
||||||
);
|
);
|
||||||
app.model(Note, {dataSource: 'db'});
|
app.model(Note, {dataSource: 'db'});
|
||||||
|
|
||||||
|
var subNote = app.registry.createModel(
|
||||||
|
'SubNote',
|
||||||
|
{text: 'string'}
|
||||||
|
);
|
||||||
|
app.model(subNote, {dataSource: 'db'});
|
||||||
|
|
||||||
var Chapter = app.registry.createModel(
|
var Chapter = app.registry.createModel(
|
||||||
'Chapter',
|
'Chapter',
|
||||||
{name: 'string'},
|
{name: 'string'},
|
||||||
|
@ -1478,6 +1484,7 @@ describe('relations - integration', function() {
|
||||||
Book.hasMany(Page);
|
Book.hasMany(Page);
|
||||||
Book.hasMany(Chapter);
|
Book.hasMany(Chapter);
|
||||||
Page.hasMany(Note);
|
Page.hasMany(Note);
|
||||||
|
Note.hasMany(subNote);
|
||||||
Chapter.hasMany(Note);
|
Chapter.hasMany(Note);
|
||||||
Image.belongsTo(Book);
|
Image.belongsTo(Book);
|
||||||
|
|
||||||
|
@ -1488,7 +1495,9 @@ describe('relations - integration', function() {
|
||||||
|
|
||||||
Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'}});
|
Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'}});
|
||||||
|
|
||||||
|
Page.nestRemoting('notes');
|
||||||
Book.nestRemoting('pages');
|
Book.nestRemoting('pages');
|
||||||
|
// Book.nestRemoting('notes');
|
||||||
Book.nestRemoting('chapters');
|
Book.nestRemoting('chapters');
|
||||||
Image.nestRemoting('book');
|
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) {
|
it('has a basic error handler', function(done) {
|
||||||
var test = this;
|
var test = this;
|
||||||
this.get('/api/books/unknown/pages/' + test.page.id + '/notes')
|
this.get('/api/books/unknown/pages/' + test.page.id + '/notes')
|
||||||
|
|
Loading…
Reference in New Issue