Skip to content

Commit

Permalink
Added async submit function..
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Nov 5, 2021
1 parent 3ec8b31 commit 4f1146d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions resources/assets/js/mixins/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
44 changes: 44 additions & 0 deletions resources/assets/js/plugins/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 4f1146d

Please sign in to comment.