Esporre azioni tramite un unico endpoint API
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)
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() });
}
});exports.install = function() {
ROUTE('API /api/ +math_add --> Math/add');
ROUTE('API /api/ -math_time --> Math/time');
};