Skip to content

Instantly share code, notes, and snippets.

@edg-l
Last active February 4, 2018 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edg-l/54196c11cc25004a1fbc353cdc50723e to your computer and use it in GitHub Desktop.
Save edg-l/54196c11cc25004a1fbc353cdc50723e to your computer and use it in GitHub Desktop.
/*
Copyright 2018 Ryozuki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* A Generic Form Handler Class
*/
class FormHandler {
/**
* The form constructor
* @param {*} formID The form id
* @param {*} url The url to perfom the action on.
* @param {*} type The http request type, e.g POST, GET, PUT
*/
constructor (formID, url, type) {
this.id = formID
this.url = url
this.type = type
this.form = $(this.id)
this.onSuccessCallbacks = []
this.onFailureCallbacks = []
this.onInternalCallbacks = []
this.onGeneralCallbacks = []
this.beforeSendCallbacks = []
if (!this.form.length) {
throw new Error('Error finding the form DOM.')
}
// Subscribe to form submit event
this.form.submit(e => {
e.preventDefault()
let ajax = this.buildAjax()
$.ajax(ajax)
})
}
buildAjax () {
return {
url: this.url,
type: this.type,
data: this.objectifyForm(),
beforeSend: () => {
this.beforeSendCallbacks.forEach(x => x(this))
},
success: (res, status, xhr) => {
this.onSuccessCallbacks.forEach(x => x(xhr.status, xhr))
this.onGeneralCallbacks.forEach(x => x(xhr.status, xhr))
},
error: (res) => {
if (res.status === 500) {
this.onInternalCallbacks.forEach(x => x(res.status, res))
}
this.onFailureCallbacks.forEach(x => x(res.status, res))
this.onGeneralCallbacks.forEach(x => x(res.status, res))
}
}
}
objectifyForm () {
var formArray = this.form.serializeArray()
var returnArray = {}
for (var i = 0; i < formArray.length; i++) {
returnArray[formArray[i]['name']] = formArray[i]['value']
}
return returnArray
}
/**
* Executed before sending the request
* @param {Function} callback The callback, a FormHandler is passed as argument
*/
beforeSend (callback) {
this.beforeSendCallbacks.push(callback)
}
/**
* Called when a response, callback args are (code, response)
* @param {function} callback The callback
*/
onResponse (callback) {
this.onGeneralCallbacks.push(callback)
}
/**
* Called on a success http response, callback args are (code, response)
* @param {function} callback The callback
*/
onSuccess (callback) {
this.onSuccessCallbacks.push(callback)
}
/**
* Called on a failure http response, callback args are (code, response)
* @param {function} callback The callback
*/
onFailure (callback) {
this.onFailureCallbacks.push(callback)
}
/**
* Called on a internal error http response, callback args are (code, response)
* @param {function} callback The callback
*/
onInternal (callback) {
this.onInternalCallbacks.push(callback)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment