diff options
Diffstat (limited to 'public/assets/js/vendor/view/formview.js')
| -rw-r--r-- | public/assets/js/vendor/view/formview.js | 215 |
1 files changed, 109 insertions, 106 deletions
diff --git a/public/assets/js/vendor/view/formview.js b/public/assets/js/vendor/view/formview.js index 21f3170..32463c8 100644 --- a/public/assets/js/vendor/view/formview.js +++ b/public/assets/js/vendor/view/formview.js @@ -1,169 +1,172 @@ var FormView = View.extend({ - method: "post", useMinotaur: false, events: { - "submit form": "save" + "submit form": "save", }, - initialize: function(opt){ + initialize: function (opt) { if (opt && opt.parent) { - this.parent = opt.parent + this.parent = opt.parent; } - this.$form = this.$("form") - this.$errors = this.$(".errors") + this.opt = opt; + this.$form = this.$("form"); + this.$errors = this.$(".errors"); }, - reset: function(){ - this.$("input,textarea").not("[type='submit']").not("[type='hidden']").val("") + reset: function () { + this.$("input,textarea") + .not("[type='submit']") + .not("[type='hidden']") + .val(""); }, - - showErrors: function(errors){ + + showErrors: function (errors) { if (errors && errors.length) { this.$errors.empty(); for (var i in errors) { - this.$errors.append('<div>' + errors[i] + '</div>'); + this.$errors.append("<div>" + errors[i] + "</div>"); } this.$errors.css("opacity", 1.0); - setTimeout(function(){ - this.$errors.show().css("opacity", 1.0); - }.bind(this), 200) + setTimeout( + function () { + this.$errors.show().css("opacity", 1.0); + }.bind(this), + 200 + ); } }, - - serialize: function(){ - var fd, fh, hasCSRF = false - var file_els = this.$("input[name][type='file']") + serialize: function () { + var fd, + fh, + hasCSRF = false; + + var file_els = this.$("input[name][type='file']"); if (file_els.length) { - fd = new FormData() - } - else { - fh = {} + fd = new FormData(); + } else { + fh = {}; } - this.$("input[name], select[name], textarea[name]").each( function(){ + this.$("input[name], select[name], textarea[name]").each(function () { if (this.type == "file") { for (var i = 0; i < this.files.length; i++) { fd.append(this.name, this.files[i]); } - } - else { + } else { if (fd) { - fd.append(this.name, this.value) + fd.append(this.name, this.value); } else { - fh[this.name] = this.value + fh[this.name] = this.value; } - hasCSRF = hasCSRF || this.name == "_csrf" + hasCSRF = hasCSRF || this.name == "_csrf"; } }); - - if (! hasCSRF) { + + if (!hasCSRF) { if (fd) { - fd.append("_csrf", $("[name=_csrf]").attr("value")) + fd.append("_csrf", $("[name=_csrf]").attr("value")); } else { - fh["_csrf"] = $("[name=_csrf]").attr("value") + fh["_csrf"] = $("[name=_csrf]").attr("value"); } } - - return fd || JSON.stringify(fh) + + return fd || JSON.stringify(fh); }, - - save: function(e, successCallback, errorCallback){ - e && e.preventDefault() + + save: function (e, successCallback, errorCallback) { + e && e.preventDefault(); this.$errors.hide().css("opacity", 0.0); if (this.validate) { - var errors = this.validate() + var errors = this.validate(); if (errors && errors.length) { if (errorCallback) { - errorCallback(errors) - } - else { - this.showErrors(errors) + errorCallback(errors); + } else { + this.showErrors(errors); } - return + return; } } - - var action = typeof this.action == "function" ? this.action() : this.action - if (! action) return - var data = this.serialize() - var headers = new Headers() - headers.append("csrf-token", $("[name=_csrf]").attr("value")) + var action = typeof this.action == "function" ? this.action() : this.action; + if (!action) return; + + var data = this.serialize(); + var headers = new Headers(); + headers.append("csrf-token", $("[name=_csrf]").attr("value")); if (typeof data === "string") { - headers.append("content-type", "application/json") + headers.append("content-type", "application/json"); } - - this.$form.addClass('sending') + + this.$form.addClass("sending"); fetch(action, { method: this.method.toUpperCase(), headers: headers, - credentials: 'same-origin', + credentials: "same-origin", body: data, - }).then(raw => raw.json()) - .then(response => { - this.$form.removeClass('sending') - if (response.error) { - var errors = [] - if (response.error.errors && response.error.errors.length) { - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); + }) + .then((raw) => raw.json()) + .then((response) => { + this.$form.removeClass("sending"); + if (response.error) { + var errors = []; + if (response.error.errors && response.error.errors.length) { + for (var key in response.error.errors) { + errors.push(response.error.errors[key].message); + } + } else { + errors.push(response.error); } - } else { - errors.push(response.error) + if (errorCallback) { + errorCallback(errors); + } else { + this.showErrors(errors); + } + return; } - if (errorCallback) { - errorCallback(errors) + if (successCallback) { + successCallback(response); } - else { - this.showErrors(errors) + if (this.success) { + this.success(response); } - return - } - if (successCallback) { - successCallback(response) - } - if (this.success) { - this.success(response) - } - }).catch(response => { - console.log(response) - this.$form.removeClass('sending') - var errors = [] - if (response.error) { - if (response.error.errors && response.error.errors.length) { - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); + }) + .catch((response) => { + console.log(response); + this.$form.removeClass("sending"); + var errors = []; + if (response.error) { + if (response.error.errors && response.error.errors.length) { + for (var key in response.error.errors) { + errors.push(response.error.errors[key].message); + } + } else { + errors.push(response.error); } } - else { - errors.push(response.error) + if (errorCallback) { + errorCallback(errors); + } else { + this.showErrors(errors); } - } - if (errorCallback) { - errorCallback(errors) - } - else { - this.showErrors(errors) - } - }) -// complete: function(response){ -// if (this.useMinotaur) { -// Minotaur.hide() -// } -// } -// -// if (this.useMinotaur) { -// Minotaur.show() -// } - - this.beforeSend && this.beforeSend() - }, + }); + // complete: function(response){ + // if (this.useMinotaur) { + // Minotaur.hide() + // } + // } + // + // if (this.useMinotaur) { + // Minotaur.show() + // } -}) + this.beforeSend && this.beforeSend(); + }, +}); /* |
