fixup! work in progress multi-level nest-remoting

This commit is contained in:
David Cheung 2017-01-06 16:12:02 -05:00
parent f0e3c913d8
commit 2d366180ae
2 changed files with 50 additions and 3 deletions

View File

@ -714,6 +714,7 @@ module.exports = function(registry) {
*/
Model.nestRemoting = function(relationName, options, filterCallback) {
if (typeof options === 'function' && !filterCallback) {
filterCallback = options;
options = {};
@ -727,9 +728,9 @@ module.exports = function(registry) {
var sharedClass = this.sharedClass;
var sharedToClass = relation.modelTo.sharedClass;
var toModelName = relation.modelTo.modelName;
console.log('nest remoting: sharedClass->toModelName %s->%s', sharedClass.name, relationName);
var pathName = options.pathName || relation.options.path || relationName;
var paramName = options.paramName || 'nk';
var paramName = options.paramName || relation.keyThrough;
var http = [].concat(sharedToClass.http || [])[0];
var httpPath, acceptArgs;
@ -761,6 +762,21 @@ module.exports = function(registry) {
}
};
//nestednested
function nestednested(method, relation) {
var multipleNestedRegex = /^__([^_]+)__([^_]+)__([^_]+)$/;
var matches = method.name.match(multipleNestedRegex);
if (matches) {
var methStr = matches.shift();
var action = matches.shift();
var searchMethName = '__' + action + '__' + matches.join('__');
var newMethdName = '__' + action + '__' + relation.name + '__' + matches.join('__');
return [searchMethName, newMethdName];
// Books.prototype.__deleteById__pages__notes__subNotes;
}
}
function buildNestedRemoteMethodOpts(method) {
var opts = {};
@ -842,6 +858,37 @@ module.exports = function(registry) {
});
}, method.isStatic);
}
} else if (!method.isStatic && (methodName = nestednested(method, relation))) {
console.log('deep nested methods', methodName[0]);
var opts = buildNestedRemoteMethodOpts(method);
console.log(opts);
var relatedRemoteMethods = sharedClass.ctor.app._remotes._classes[relation.modelTo.modelName]._methods;
var nestedFn = relatedRemoteMethods.filter(function(sharedMeth) {
return (sharedMeth.name == methodName[0]);
})[0];
if (relation.multiple) {
// console.log('methodName: %s', methodName);
sharedClass.defineMethod(methodName[1], opts, function(fkId) {
var args = Array.prototype.slice.call(arguments, 1);
var last = args[args.length - 1];
var cb = typeof last === 'function' ? last : null;
// var fn = sharedClass.ctor.app._remotes._classes[relation.modelTo.modelName].;
this[getterName](fkId, function(err, inst) {
if (err && cb) return cb(err);
if (inst instanceof relation.modelTo) {
try {
nestedFn.apply(inst, args);
} catch (err) {
if (cb) return cb(err);
}
} else if (cb) {
cb(err, null);
}
});
}, method.isStatic);
// console.log(sharedClass);
}
}
});

View File

@ -1604,7 +1604,7 @@ describe('relations - integration', function() {
done();
});
it('it has nested relationship routes', function(done) {
it.only('it has nested relationship routes', function(done) {
var app = this.app;
var pageMethodsStringName = app._remotes._classes.Page._methods
.map(function(item) { return item.stringName; });