Skip to content

Instantly share code, notes, and snippets.

@nshCore
Last active October 21, 2016 00:40
Show Gist options
  • Save nshCore/261fee5667efcf81648ab2a1a1c33c1b to your computer and use it in GitHub Desktop.
Save nshCore/261fee5667efcf81648ab2a1a1c33c1b to your computer and use it in GitHub Desktop.
import helpers from './helpers.js';
/**
* Ajax directive to easily submit vue forms via ajax
*
* @author James Kirkby <jkirkby91@gmail.com>
*/
export default {
params: ['complete'],
/**
* bind the function to the submit button
*/
bind: function () {
this.el.addEventListener(
'submit', this.onSubmit.bind(this)
);
},
/**
* onSubmit event function to send form data via ajax
*
* @param e
*/
onSubmit: function (e) {
e.preventDefault();
let button = this.el.querySelector('button[type="submit"]');
button.disabled = true;
let callBackFunction = button.getAttribute('data-callback');
console.log(callBackFunction);
let payload = helpers.jsonEncodeObj(helpers.serializeForm(this.el));
this.vm.$http[this.getRequestType()](this.el.action, payload)
.then(function(response){
let response_message = response.data.message;
let message = '';
if(typeof response_message === 'object')
{
for (var key in response_message) {
if (response_message.hasOwnProperty(key)) {
message += key + " : " + response_message[key] + " <br> ";
}
}
}
else
{
message = response.data.message;
}
if(response.data.status === 200)
{
formInstance.reset();
// show success message here
}
button.disabled = false;
})
},
/**
* Gets the type of http request from the form
*/
getRequestType: function () {
var method = this.el.querySelector('input[name="_method"]');
return (method ? method.value : this.el.method).toLowerCase();
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment