action list
impl autor
This commit is contained in:
parent
369128b0b1
commit
4b3142bf6b
@ -86,17 +86,14 @@ Action.prototype = Object.create(EventEmitter.prototype, {
|
||||
function ShowAction() {
|
||||
logger.debug('new ShowAction');
|
||||
Action.call(this, 'show');
|
||||
|
||||
// Resource id.
|
||||
this.resId = null;
|
||||
// Resource.
|
||||
this.res = null;
|
||||
};
|
||||
// inherits Action
|
||||
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.
|
||||
* Headers:
|
||||
@ -127,10 +124,7 @@ ShowAction.prototype = Object.create(Action.prototype, {
|
||||
//TODO error?
|
||||
callback(null, '5OO', {});
|
||||
}
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true
|
||||
}, enumerable: true, configurable: true, writable: true
|
||||
},
|
||||
/**
|
||||
* Load resource.
|
||||
@ -141,10 +135,7 @@ ShowAction.prototype = Object.create(Action.prototype, {
|
||||
value: function(resId, callback){
|
||||
//implement
|
||||
callback(new Error('Cannot load resource ' + resId));
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true
|
||||
}, enumerable: true, configurable: true, writable: true
|
||||
},
|
||||
/**
|
||||
* Get related resources.
|
||||
@ -154,10 +145,7 @@ ShowAction.prototype = Object.create(Action.prototype, {
|
||||
getRelated: {
|
||||
value: function(res){
|
||||
return [];
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true
|
||||
}, enumerable: true, configurable: true, writable: true
|
||||
},
|
||||
|
||||
doAction: {
|
||||
@ -165,15 +153,47 @@ ShowAction.prototype = Object.create(Action.prototype, {
|
||||
logger.debug('doAction');
|
||||
this.emit('data', this.res);
|
||||
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 = {
|
||||
ShowAction: ShowAction
|
||||
ShowAction: ShowAction,
|
||||
ListAction: ListAction
|
||||
};
|
||||
|
@ -23,7 +23,17 @@ Author.prototype = Object.create(Endpoint.prototype, {
|
||||
});
|
||||
};
|
||||
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:
|
||||
callback(new Error('action not implemented'));
|
||||
}
|
||||
@ -35,7 +45,8 @@ Author.prototype = Object.create(Endpoint.prototype, {
|
||||
targetCollection : {
|
||||
value: function(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
|
||||
this.authorId = match[2];
|
||||
return false;
|
||||
|
@ -15,11 +15,12 @@ Endpoint.prototype = {
|
||||
*/
|
||||
buildAction : function(method, url, callback) {
|
||||
var col = this.targetCollection(url.pathname);
|
||||
logger.debug('Building action ('+method+','+col+')');
|
||||
var action;
|
||||
if (col && method == 'POST') {
|
||||
//TODO search
|
||||
} else if (col && method == 'GET') {
|
||||
//TODO list
|
||||
action = new Action.ListAction();
|
||||
} else if (!col && method == 'POST') {
|
||||
//TODO edit
|
||||
} else if (!col && method == 'GET') {
|
||||
|
@ -60,12 +60,45 @@ Outputter.prototype = {
|
||||
var JSONOutputter = function() {
|
||||
Outputter.call(this);
|
||||
logger.debug('JSON');
|
||||
this.buffer = new Array();
|
||||
this.colStarted = false;
|
||||
};
|
||||
// inherits Outputter
|
||||
JSONOutputter.prototype = Object.create(Outputter.prototype, {
|
||||
output: {
|
||||
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
|
||||
},
|
||||
init: {
|
||||
|
Loading…
Reference in New Issue
Block a user