Permalink
Browse files

[api] Added support for one-many resourceful relationships

  • Loading branch information...
1 parent 17c7ddb commit 188665a67650a5c50b739e9966eea12797024069 @Marak Marak committed Aug 28, 2012
Showing with 51 additions and 9 deletions.
  1. +51 −9 lib/engines/socketio.js
View
60 lib/engines/socketio.js
@@ -15,18 +15,14 @@ engine.createServer = function (resources, options, callback) {
// Remark: Delegate the resource action to the appropiate engine method
//
socket.on(resource.lowerResource, function (action, payload, callback) {
+
//
- // CRUD methods
+ // Resource methods
//
if(typeof resource[action] === 'function') {
return engine.request(resource, action, payload, callback);
}
- //
- // Abritrary remote methods
- //
- if(typeof resource[action] === 'function' && resource[action].remote) {
- return engine.request(resource, action, payload, callback);
- }
+
return callback(new Error(action + ' is not a valid action.'));
});
});
@@ -43,7 +39,9 @@ engine.request = function(resource, action, payload, callback) {
//
switch (action) {
case 'create':
- resource[action](payload, callback);
+ resource[action](payload, function(err, result){
+ callback(err, result);
+ });
break;
case 'get':
resource[action](payload, callback);
@@ -58,7 +56,51 @@ engine.request = function(resource, action, payload, callback) {
resource[action](payload.id, callback);
break;
default:
- resource[action](payload.id, payload, callback);
+ //
+ // Remark: an abritrary remote method, intended to be exposed
+ //
+ if(resource[action].remote === true) {
+ resource[action](payload, callback);
+ } else {
+ //
+ // Remark: Determine if it's any child methods
+ //
+ var _action = action.substr(0, 3);
+ switch (_action) {
+ case 'get':
+ //
+ // getChild
+ //
+ resource.get(payload.album_id, function(err, child){
+ if(err) {
+ return callback(err);
+ }
+ child[action](payload, callback);
+ });
+
+ resource[action](payload, callback);
+ break;
+ case 'cre':
+ //
+ // createChild
+ //
+
+ //
+ // Remark: must get instance of parent before creating child
+ //
+ // see: https://github.com/flatiron/resourceful/issues/110
+ //
+ resource.get(payload.album_id, function(err, child){
+ if(err) {
+ return callback(err);
+ }
+ child[action](payload, callback);
+ });
+ break;
+ default:
+ break;
+ }
+ }
break;
}
}

0 comments on commit 188665a

Please sign in to comment.