add submitHandler helper function to asyncform util
This commit is contained in:
parent
e9f7cdf682
commit
f798142a29
@ -12,31 +12,7 @@
|
||||
var lastRequestTimestamp = 0;
|
||||
|
||||
function setup() {
|
||||
formElement.addEventListener('submit', function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
formElement.classList.add(ASYNC_FORM_LOADING_CLASS)
|
||||
lastRequestTimestamp = Date.now();
|
||||
|
||||
var url = formElement.getAttribute('action');
|
||||
var headers = { };
|
||||
var body = new FormData(formElement);
|
||||
|
||||
if (options && options.headers) {
|
||||
Object.keys(options.headers).forEach(function(headerKey) {
|
||||
headers[headerKey] = options.headers[headerKey];
|
||||
});
|
||||
}
|
||||
|
||||
window.utils.httpClient.post(url, headers, body)
|
||||
.then(function(response) {
|
||||
return response.json();
|
||||
}).then(function(response) {
|
||||
processResponse(response[0])
|
||||
}).catch(function(error) {
|
||||
console.error('could not fetch or process response from ' + url, { error });
|
||||
});
|
||||
});
|
||||
formElement.addEventListener('submit', submitHandler);
|
||||
}
|
||||
|
||||
function processResponse(response) {
|
||||
@ -51,7 +27,32 @@
|
||||
parentElement.insertBefore(responseElement, formElement);
|
||||
formElement.remove();
|
||||
}, delay);
|
||||
}
|
||||
|
||||
function submitHandler(event) {
|
||||
event.preventDefault();
|
||||
|
||||
formElement.classList.add(ASYNC_FORM_LOADING_CLASS)
|
||||
lastRequestTimestamp = Date.now();
|
||||
|
||||
var url = formElement.getAttribute('action');
|
||||
var headers = { };
|
||||
var body = new FormData(formElement);
|
||||
|
||||
if (options && options.headers) {
|
||||
Object.keys(options.headers).forEach(function(headerKey) {
|
||||
headers[headerKey] = options.headers[headerKey];
|
||||
});
|
||||
}
|
||||
|
||||
window.utils.httpClient.post(url, headers, body)
|
||||
.then(function(response) {
|
||||
return response.json();
|
||||
}).then(function(response) {
|
||||
processResponse(response[0])
|
||||
}).catch(function(error) {
|
||||
console.error('could not fetch or process response from ' + url, { error });
|
||||
});
|
||||
}
|
||||
|
||||
setup();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user