diff options
Diffstat (limited to 'public/assets/javascripts/vendor/ModalFormView.js')
| -rw-r--r-- | public/assets/javascripts/vendor/ModalFormView.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/public/assets/javascripts/vendor/ModalFormView.js b/public/assets/javascripts/vendor/ModalFormView.js new file mode 100644 index 0000000..16d63b2 --- /dev/null +++ b/public/assets/javascripts/vendor/ModalFormView.js @@ -0,0 +1,76 @@ + +var ModalFormView = ModalView.extend({ + + method: "put", + + events: { + "submit form": "submit" + }, + + initialize: function(){ + this.$form = this.$("form") + this.$errors = this.$(".errors") + this.$errorList = this.$(".errorList") + }, + + reset: function(){ + this.$("input").not("[type='submit']").not("[type='hidden']").val("") + }, + + load: function(){ + this.reset() + this.show() + }, + + showErrors: function(errors){ + if (errors && errors.length) { + this.$errors.show(); + for (var i in errors) { + this.$errorList.append('<div>' + errors[i] + '</div>'); + } + } + }, + + submit: function(e){ + e.preventDefault() + + this.$errors.hide(); + this.$errorList.empty(); + + if (this.validate) { + var errors = this.validate() + if (errors && errors.length) { + this.showErrors(errors) + return + } + } + + var fields = this.$form.serializeArray() + fields.forEach(function(pair){ + if (pair.name.indexOf("password") !== -1 && pair.value.length > 0) { + pair.value = SHA1.hex('lol$' + pair.value + '$vvalls') + } + }) + + var request = $.ajax({ + url: this.action, + type: this.method, + data: $.param(fields) + }); + request.done($.proxy(function (response) { + if (response.error) { + this.$errors.show(); + 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)); + } + +}) |
