Added async submit function..

This commit is contained in:
Cüneyt Şentürk 2021-11-05 19:16:04 +03:00
parent 3ec8b3120b
commit 4f1146dc33
2 changed files with 49 additions and 0 deletions

View File

@ -119,6 +119,11 @@ export default {
this.form.submit();
},
// Form Async Submit
async onAsyncSubmit() {
await this.form.asyncSubmit();
},
onHandleFileUpload(key, event) {
this.form[key] = '';
this.form[key] = event.target.files[0];

View File

@ -409,6 +409,50 @@ export default class Form {
.catch(this.onFail.bind(this));
}
async asyncSubmit() {
FormData.prototype.appendRecursive = function(data, wrapper = null) {
for (var name in data) {
if (name == "previewElement" || name == "previewTemplate") {
continue;
}
if (wrapper) {
if ((typeof data[name] == 'object' || Array.isArray(data[name])) && ((data[name] instanceof File != true ) && (data[name] instanceof Blob != true))) {
this.appendRecursive(data[name], wrapper + '[' + name + ']');
} else {
this.append(wrapper + '[' + name + ']', data[name]);
}
} else {
if ((typeof data[name] == 'object' || Array.isArray(data[name])) && ((data[name] instanceof File != true ) && (data[name] instanceof Blob != true))) {
this.appendRecursive(data[name], name);
} else {
this.append(name, data[name]);
}
}
}
};
this.loading = true;
let data = this.data();
let form_data = new FormData();
form_data.appendRecursive(data);
await window.axios({
method: this.method,
url: this.action,
data: form_data,
headers: {
'X-CSRF-TOKEN': window.Laravel.csrfToken,
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'multipart/form-data'
}
})
.then(this.onSuccess.bind(this))
.catch(this.onFail.bind(this));
}
onSuccess(response) {
this.errors.clear();