var FormView = View.extend({ method: "post", events: { "submit form": "save" }, initialize: function(opt){ if (opt && opt.parent) { this.parent = opt.parent } this.$form = this.$("form") this.$errors = this.$(".errors") this.$errorList = this.$(".errorList") }, reset: function(){ this.$("input,textarea").not("[type='submit']").not("[type='hidden']").val("") }, showErrors: function(errors){ if (errors && errors.length) { this.$errorList.empty(); for (var i in errors) { this.$errorList.append('
' + errors[i] + '
'); } this.$errors.css("opacity", 1.0); setTimeout(function(){ this.$errors.show().css("opacity", 1.0); }.bind(this), 200) } }, serialize: function(){ var fd = new FormData() this.$("input[name], select[name], textarea[name]").each( function(){ if (this.type == "file") { if (this.files.length > 0) { fd.append(this.name, this.files[0]); } } else if (this.type == "password") { if (this.value.length > 0) { fd.append(this.name, SHA1.hex('lol$' + this.value + '$vvalls')) } } else { fd.append(this.name, this.value); } }); return fd }, save: function(e){ e.preventDefault() this.$errors.hide().css("opacity", 0.0); if (this.validate) { var errors = this.validate() if (errors && errors.length) { this.showErrors(errors) return } } var request = $.ajax({ url: this.action, type: this.method, data: this.serialize(), dataType: "json", processData: false, contentType: false, }); request.done($.proxy(function (response) { if (response.error) { var errors = [] for (var key in response.error.errors) { errors.push(response.error.errors[key].message); } this.showErrors(errors) return } else { this.success && this.success(response) } }, this)); } }) var ModalFormView = ModalView.extend(FormView.prototype).extend({ load: function(){ this.reset() this.show() } })