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, headers, verb, params)
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.
header when provided must be an object encoding custom headers. More specifically, properties are header names, while their values are header contents.
verb specifies the verb to use for the request. It defaults to "GET".
params when provided specifies parameters to send in url encoded format(par1=value1&par2=val2....). Usually, "POST" requests include parameters, while "GET" requests put them in the URL.
.postForm(inForm, url, extraData, onSuccess, errorMessageF, onError, onCompleted, onProgress, onStart, headers)
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.
header when provided must be an object encoding custom headers. More specifically, properties are header names, while their values are header contents.
All other parameters have the same meaning as in the getContent method.
.postJson(el, url, data, bearerToken, onSuccess, errorMessageF, onError, onCompleted, onProgress, headers)
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, bearerToken)
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.
bearerToken when provided specifies a bearer token to add to each endpoint request.
.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