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.elis the Html node whose content might be affected when the operation is completed.elis passed as the unique argument toonCompletedthat is called when the operation is succesfully or unsuccesfully completed.onCompletedmay be used to remove an user interaction block previously applied toelbefore callinggetContent. If not nullbearerTokenis added to the header as bearer token (in order to authenticate the request).onErroris 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 toerrorMessageFand taking the result returned. In case of success the whole response is passed toonSuccessas a string.onProgressis used asonprogresproperty of theXMLHttpRequestobject that performs the ajax call. Thus, it receives an event object with the progress state as its unique argument.
headerwhen provided must be an object encoding custom headers. More specifically, properties are header names, while their values are header contents.
verbspecifies the verb to use for the request. It defaults to "GET".
paramswhen 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 ininFormwith an ajax call. Ifurlis 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.onStartis called immediately before starting the ajax operation and is passedinFormas its unique argument.
headerwhen 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 thegetContentmethod. - .postJson(el, url, data, bearerToken, onSuccess, errorMessageF, onError, onCompleted, onProgress, headers)
-
posts
datatoulrafter having converted it into a Json string. All other parameters have the same meaning as in thegetContentmethod.
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)
-
nameis 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 toonSuccess. All other parameters have the same meaning as inpostForm. The node passed to bothonStartedandonCompletedis the link itself.
bearerTokenwhen 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.