Permalink
Browse files

[refactor] [minor] Better delegation of incoming requests. Made room …

…for relational resources in socket mapping API.
  • Loading branch information...
Marak committed Aug 28, 2012
1 parent 41b2a88 commit 690f67a6abdc6f867ecaaac6ab2b8cfc11477d4e
Showing with 29 additions and 28 deletions.
  1. +29 −28 lib/engines/socketio.js
View
@@ -11,27 +11,22 @@ engine.createServer = function (resources, options, callback) {
//
// For every resource, create a new socket.io handler
//
- // console.log('mapping', resource.lowerResource)
-
//
// Remark: Delegate the resource action to the appropiate engine method
//
socket.on(resource.lowerResource, function (action, payload, callback) {
-
//
// CRUD methods
//
- if(typeof engine[action] === 'function') {
- return engine[action](resource, payload, callback);
+ if(typeof resource[action] === 'function') {
+ return engine.request(resource, action, payload, callback);
}
-
//
// Abritrary remote methods
//
- if(typeof resource[action] === 'function' && resource[action].remote) {
- return resource[action](payload.id, payload, callback);
+ 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.'));
});
});
@@ -42,22 +37,28 @@ engine.createServer = function (resources, options, callback) {
return io;
};
-engine.create = function (resource, payload, callback) {
- resource.create(payload, callback);
-};
-
-engine.get = function (resource, payload, callback) {
- resource.get(payload, callback);
-};
-
-engine.all = function (resource, payload, callback) {
- resource.all(callback);
-};
-
-engine.update = function (resource, payload, callback) {
- resource.update(payload.id, payload, callback);
-};
-
-engine.destroy = function (resource, payload, callback) {
- resource.destroy(payload.id, callback);
-};
+engine.request = function(resource, action, payload, callback) {
+ //
+ // Remark: Perform a switch/case to determine Resource method call signature
+ //
+ switch (action) {
+ case 'create':
+ resource[action](payload, callback);
+ break;
+ case 'get':
+ resource[action](payload, callback);
+ break;
+ case 'all':
+ resource[action](callback);
+ break;
+ case 'update':
+ resource[action](payload.id, payload, callback);
+ break;
+ case 'destroy':
+ resource[action](payload.id, callback);
+ break;
+ default:
+ resource[action](payload.id, payload, callback);
+ break;
+ }
+}

0 comments on commit 690f67a

Please sign in to comment.