From f798142a2965ef68fc4c14cc262c69fa1196efc4 Mon Sep 17 00:00:00 2001 From: Felix Hamann Date: Mon, 25 Feb 2019 20:41:10 +0100 Subject: [PATCH] add submitHandler helper function to asyncform util --- static/js/utils/asyncForm.js | 51 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/static/js/utils/asyncForm.js b/static/js/utils/asyncForm.js index 14e77c805..7917012a9 100644 --- a/static/js/utils/asyncForm.js +++ b/static/js/utils/asyncForm.js @@ -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();