Manage AJAX requests and HTML form submittion through AJAX.
Dependencies:
- None
Methods
(static) addInJson(json, key, value) → {Object}
Add the specific value in the json usign the specified key
portal.ajax.addInJson({}, "key", 123);
// Produce {"key": 123}
portal.ajax.addInJson({}, "key[subKey]", 123);
// Produce
// {
// "key": {
// "subKey": 123
// }
// }
portal.ajax.addInJson({}, "key[subKey[subsubKey]]", 123);
// Produce
// {
// "key": {
// "subKey": {
// "subsubKey": 123
// }
// }
// }
Parameters:
Name | Type | Description |
---|---|---|
json |
Object | JSON object to update |
key |
String | Key to add |
value |
Object | Value of the key to add |
Returns:
Updated JSON
- Type
- Object
(static) batch(requests, optionsopt) → {Promise}
Execute several AJAX requests an return a promise with the results in JSON format of each requests.
portal.ajax.batch([
{url : "http://my.first.url/", options : {method : "GET"}},
{url : "http://my.second.url/", options : {method : "DELETE"}},
], {
success : function (request, response) {
console.log(reqest.url + " succeed");
},
error : function (request, response) {
console.log(reqest.url + " failed");
}
}).then((responses) => {
// when all requests are done
}).catch((responses) => {
// When all requests failed
});
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
requests |
Array.<Object> | Array of requests to execute Properties
|
|||||||||||||||||||||||||||||||||||||
options |
Object |
<optional> |
Options for the batch requests Properties
|
Returns:
If all the requests failed, the promise will failed.
- Type
- Promise
(static) fetch(url, options) → {Promise}
Execute an AJAX request and return a promise with the result in JSON format.
portal.ajax.fetch("//my/urls", {method : "GET"}).then(json => {
console.log("RESULT:", json);
}).catch(error => {
console.error(error);
});
Parameters:
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
url |
String | Url to request |
||||||||||||||||||||
options |
Object | Options for the request (look at the documentation of Fetch) Properties
|
Returns:
Promise with the result in JSON format
- Type
- Promise
(static) formToJson(elForm, optionsopt) → {Object}
Serialize the form to an JSON object
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
elForm |
HTMLElement | Form element to parse |
|||||||||
options |
Options |
<optional> |
Option to perform the action Properties
|
Returns:
Return the form data in JSON format
- Type
- Object
(static) makeAllFormsAjax(optionsopt)
Make all forms in the page submittable through AJAX request.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
<optional> |
Options to attach on all forms (events). Properties
|
(static) makeFormAjax(elForm, optionsopt)
Make the specified forms submittable with AJAX request.
This method use the method
attribute of the HTML Form as the method
of the AJAX request.
const form = document.getElementById("my-form");
portal.ajax.makeFormAjax(form);
With options:
const form = document.getElementById("my-form");
portal.ajax.makeFormAjax(form, {
beforeSubmit : () => {
console.log("I will submit....")
},
submitSuccess : (event) => {
console.log("The result is:", event.detail)
},
submitError: (event) => {
console.error(event.detail);
}
});
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
elForm |
HTMLElement | Form element to listen. |
|||||||||||||||||||||
options |
Object |
<optional> |
Options to attach on current form (events). Properties
|
(static) submitForm(elForm, optionsopt) → {Promise}
Submit the specified HTML form using the AJAX request.
This method use the method
attribute of the HTML Form as the method
of the AJAX request.
Parameters:
Name | Type | Attributes | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
elForm |
HTMLElement | Form element to submit |
|||||||||
options |
Options |
<optional> |
Option to perform the action Properties
|
Returns:
Promise with the result in JSON format
- Type
- Promise