action list

impl autor
This commit is contained in:
Meutel 2014-01-22 19:38:47 +01:00
parent 369128b0b1
commit 4b3142bf6b
4 changed files with 95 additions and 30 deletions

View File

@ -86,17 +86,14 @@ Action.prototype = Object.create(EventEmitter.prototype, {
function ShowAction() { function ShowAction() {
logger.debug('new ShowAction'); logger.debug('new ShowAction');
Action.call(this, 'show'); Action.call(this, 'show');
// Resource id.
this.resId = null;
// Resource.
this.res = null;
}; };
// inherits Action // inherits Action
ShowAction.prototype = Object.create(Action.prototype, { ShowAction.prototype = Object.create(Action.prototype, {
/**
* Resource id.
*/
resId: { value: null, enumerable: true, configurable: true, writable: true },
/**
* Resource.
*/
res: { value: null, enumerable: true, configurable: true, writable: true },
/** /**
* Prepare show action. * Prepare show action.
* Headers: * Headers:
@ -127,10 +124,7 @@ ShowAction.prototype = Object.create(Action.prototype, {
//TODO error? //TODO error?
callback(null, '5OO', {}); callback(null, '5OO', {});
} }
}, }, enumerable: true, configurable: true, writable: true
enumerable: true,
configurable: true,
writable: true
}, },
/** /**
* Load resource. * Load resource.
@ -141,10 +135,7 @@ ShowAction.prototype = Object.create(Action.prototype, {
value: function(resId, callback){ value: function(resId, callback){
//implement //implement
callback(new Error('Cannot load resource ' + resId)); callback(new Error('Cannot load resource ' + resId));
}, }, enumerable: true, configurable: true, writable: true
enumerable: true,
configurable: true,
writable: true
}, },
/** /**
* Get related resources. * Get related resources.
@ -154,10 +145,7 @@ ShowAction.prototype = Object.create(Action.prototype, {
getRelated: { getRelated: {
value: function(res){ value: function(res){
return []; return [];
}, }, enumerable: true, configurable: true, writable: true
enumerable: true,
configurable: true,
writable: true
}, },
doAction: { doAction: {
@ -165,15 +153,47 @@ ShowAction.prototype = Object.create(Action.prototype, {
logger.debug('doAction'); logger.debug('doAction');
this.emit('data', this.res); this.emit('data', this.res);
this.emit('end'); this.emit('end');
}, }, enumerable: true, configurable: true, writable: true
enumerable: true,
configurable: true,
writable: true
} }
});
/**
* Action show list of resources.
*/
function ListAction() {
logger.debug('new ListAction');
Action.call(this, 'list');
};
// inherits Action
ListAction.prototype = Object.create(Action.prototype, {
prepare: {
value: function(callback) {
//TODO
callback(null, 200, {});
}, enumerable: true, configurable: true, writable: true
},
doAction: {
value: function() {
logger.debug('doAction');
var self = this;
// TODO parameters
this.loadResources(function(err, res) {
// for each loaded resource
self.emit('data', res);
}, function(err) {
self.emit('end');
});
}, enumerable: true, configurable: true, writable: true
},
loadResources: {
value: function(onload, onend) {
// nothing to load
onend(null);
}, enumerable: true, configurable: true, writable: true
}
}); });
module.exports = { module.exports = {
ShowAction: ShowAction ShowAction: ShowAction,
ListAction: ListAction
}; };

View File

@ -23,7 +23,17 @@ Author.prototype = Object.create(Endpoint.prototype, {
}); });
}; };
callback(null, action); callback(null, action);
break; break;
case 'list':
action.loadResources = function(onload, onend) {
//TODO load from bdd
onload(null, { id: 1, name: 'Homere' });
onload(null, { id: 2, name: 'Victor Hugo' });
onload(null, { id: 3, name: 'Frank Herbert' });
onend();
};
callback(null, action);
break;
default: default:
callback(new Error('action not implemented')); callback(new Error('action not implemented'));
} }
@ -35,7 +45,8 @@ Author.prototype = Object.create(Endpoint.prototype, {
targetCollection : { targetCollection : {
value: function(pathname) { value: function(pathname) {
var match = PATH_RE.exec(pathname); var match = PATH_RE.exec(pathname);
if (match.length > 2) { logger.debug('pathname ' + pathname + ' => ' + match);
if (match.length > 2 && match[2]) {
// TODO check integer // TODO check integer
this.authorId = match[2]; this.authorId = match[2];
return false; return false;

View File

@ -15,11 +15,12 @@ Endpoint.prototype = {
*/ */
buildAction : function(method, url, callback) { buildAction : function(method, url, callback) {
var col = this.targetCollection(url.pathname); var col = this.targetCollection(url.pathname);
logger.debug('Building action ('+method+','+col+')');
var action; var action;
if (col && method == 'POST') { if (col && method == 'POST') {
//TODO search //TODO search
} else if (col && method == 'GET') { } else if (col && method == 'GET') {
//TODO list action = new Action.ListAction();
} else if (!col && method == 'POST') { } else if (!col && method == 'POST') {
//TODO edit //TODO edit
} else if (!col && method == 'GET') { } else if (!col && method == 'GET') {

View File

@ -60,12 +60,45 @@ Outputter.prototype = {
var JSONOutputter = function() { var JSONOutputter = function() {
Outputter.call(this); Outputter.call(this);
logger.debug('JSON'); logger.debug('JSON');
this.buffer = new Array();
this.colStarted = false;
}; };
// inherits Outputter // inherits Outputter
JSONOutputter.prototype = Object.create(Outputter.prototype, { JSONOutputter.prototype = Object.create(Outputter.prototype, {
output: { output: {
value: function(resource) { value: function(resource) {
this.out.write(JSON.stringify(resource)); logger.debug('ressource: '+JSON.stringify(this.buffer));
logger.debug('colStarted: '+this.colStarted);
if (!this.colStarted && this.buffer.length == 0)
this.buffer.push(resource);
else {
this.buffer.push(resource);
if (!this.colStarted) {
this.out.write('[');
this.colStarted = true;
} else
this.out.write(',');
while (this.buffer.length>0) {
var r = this.buffer.shift();
this.out.write(JSON.stringify(r));
if (this.buffer.length>0)
this.out.write(',');
}
}
}, enumerable: true, configurable: true, writable: true
},
end: {
value: function() {
if (this.buffer.length == 1) {
// single resource
this.out.write(JSON.stringify(this.buffer[0]));
}
if (this.colStarted) {
//end collection
this.out.write(']');
}
logger.debug('Action ended');
this.out.end();
}, enumerable: true, configurable: true, writable: true }, enumerable: true, configurable: true, writable: true
}, },
init: { init: {