jaxify
Generate client and server side ajax calls code.
why
- Code less. In the README.md example, I wrote an input file of 16 lines, and got two necessary files with a sum of around 30.
- It forces the developer to use the promise returned from
$.ajax
instead ofsuccess
,error
andcomplete
properties. This is better because promises make code more readable. - When you change something (ie: the url), you only have to modify one codebase. This is also true because of the previous point, ajax calls are meant to be defined once client side.
- It automatically maps parameters of
fn
that are in the url (like id in/find/:id
) toreq.params
. And, the parameters offn
that are not in the url are mapped toreq.body
. No confusion betweenreq.params
andreq.body
. And because of this, client ajax methods can be called with simple code, for example:ajax.find(1, false)
even if the first parameter is a url parameter, and the other is mapped todata
.
how
- Write a
jaxi.js
file like this:
var ajax = find: method: 'get' url: '/find/:id' { if filter ; ; } delete: method: 'delete' { ; }
-
Run
jaxify
in a terminal to generate aclient.jaxi.js
and aserver.jaxi.js
-
Look at the magic:
client.jaxi.js
var ajax = { return $ } { return $ }// $.ajax returns a jqXHR, that implements the promise interface, so you can do ajax;
server.jaxi.js
{ for var name in ajax var action = ajaxname; var url = actionurl ? actionurl : '/' + name; appactionmethodurl { action; }; } var ajax = find: method: 'get' url: '/find/:id' { var id = reqparamsid; var filter = reqbodyfilter; if filter res res; } delete: method: 'delete' { res; } moduleexports = setExpress;
- Add
client.jaxi.js
as a script to HTML. - Require
server.jaxi.js
in server and invoke with Express, for example:
var app = ;app;
install
With npm do:
npm install -g jaxify
usage
Usage: jaxify {OPTIONS}
Standard Options:
--clientfile, -c Write the client output to this file.
If unspecified, use 'client.jaxi.js'
--serverfile, -s Write the server output to this file.
If unspecified, use 'server.jaxi.js'
--inputfile, -i Use this file as input.
If unspecified, use 'jaxi.js'
--webserver, -w Use the specified webserver.
Options are 'express'.
--ajax, -a Use the specified client side ajax library.
Options are 'jquery'.
--help, -h Show this message
license
MIT