Ajax utilities

Ajax utilities are contained in the mvcct-controls package file mvcct.controls.ajax.js. When using usual synchronous loading all utilities are contained in the jJavaScript namespace mvcct.controls, with AMD or Node.js loading the object containing them may be obtained by requiring mvcct.controls.ajax.

Ajax loading utilities

.getContent(el, url, bearerToken, onSuccess, errorMessageF, onError, onCompleted, onProgress)
It perform an ajax get operation on url. el is the Html node whose content might be affected when the operation is completed. el is passed as the unique argument to onCompleted that is called when the operation is succesfully or unsuccesfully completed. onCompleted may be used to remove an user interaction block previously applied toel before calling getContent. If not null bearerToken is added to the header as bearer token (in order to authenticate the request). onError is called passing it an error message in case of any error, namely: communication error, error status code returned by the server, or in case server response starts with '#', in which case the remainder of the response is assumed to be a an error message returned by the server. In case of error status code the error message is obtained by passing the status code to errorMessageF and taking the result returned. In case of success the whole response is passed to onSuccess as a string. onProgress is used as onprogres property of the XMLHttpRequest object that performs the ajax call. Thus, it receives an event object with the progress state as its unique argument
.postForm(inForm, url, extraData, onSuccess, errorMessageF, onError, onCompleted, onProgress, onStart)
posts inForm, if, it is a form, or a form contained in inForm with an ajax call. If url is not null it is used instead of the url specified in the form. If not null extradata must be a string that specifies param/value pairs in query string format (ie like 'par1=val1&par2=val2...'); it is concatenated with the result of the serialization of all input fileds contained in the form. onStart is called immediately before starting the ajax operation and is passed inForm as its unique argument. All other parameters have the same meaning as in the getContent method.
.postJson(el, url, data, bearerToken, onSuccess, errorMessageF, onError, onCompleted, onProgress)
posts data to ulr after having converted it into a Json string. All other parameters have the same meaning as in the getContent method.

Json enddpoints

If a link is added the Html5 attribute data-operation='ajax-json endpointName' an ajax GET call to the url contained in the link, is started when the link is clicked. All call features are specified in the declaration of endpointName. Endpoints are declared with:

.addJsonEndpoint(name, bearerToken, onSuccess, errorMessageF, onError, onStart, onCompleted, onProgress)
name is the endpoint name to be referred in the link. The ajax call is expected to return Json, and in case of success data obtained by parsing the Json received are passed as unique argument to onSuccess. All other parameters have the same meaning as in postForm. The node passed to both onStarted and onCompleted is the link itself.
.removeEndpoint(name)
remove a previously defined endpoint called name. It must be called when all links referring to it have been removed from the dom, in order to avoid memory leaks.

Fork me on GitHub