ForgeTotal.js 5
it

Total.js 5

Esporre azioni tramite un unico endpoint API

Total.js 5 · total5@0.0.19 routingapiactions Eseguita con Total.js 5

Una route API pubblica azioni sotto un'unica URL. Il client invia sempre POST con un body JSON { "schema": "name", "data": { ... } } e Total.js lo smista all'azione registrata con quel nome. Una URL, molte operazioni: è lo stile che parla la funzione API() di jComponent.

Il prefisso del nome decide cosa succede a data:

  • +name: l'input dell'azione è obbligatorio e viene validato
  • -name: l'azione non accetta input
  • %name: input parziale (per le patch)
definitions/math.js
NEWACTION('Math/add', {
	name: 'Add two numbers',
	input: '*a:Number, *b:Number',
	action: function($, model) {
		$.callback({ sum: model.a + model.b });
	}
});

NEWACTION('Math/time', {
	name: 'Server time',
	action: function($) {
		$.callback({ year: new Date().getFullYear() });
	}
});
controllers/api.js
exports.install = function() {
	ROUTE('API /api/ +math_add --> Math/add');
	ROUTE('API /api/ -math_time --> Math/time');
};