var SignUpModal = ModalView.extend({ el: ".mediaDrawer.signup", action: "/auth/signup", 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() }, validate: function(){ var errors = [] var username = this.$("#usernameInput").val() var email = this.$("#emailInput").val() var pw1 = this.$("#passwordInput1").val() var pw2 = this.$("#passwordInput2").val() if (! username.length) { errors.push("Please enter a username"); } if (! pw1.length) { errors.push("Please enter a password"); } if (! email.length) { errors.push("Please enter an email address"); } else if (email.indexOf("@") === -1) { errors.push("Please enter a valid email address"); } if (pw1 !== pw2) { errors.push("Passwords don't match"); } if (errors.length) { this.$errors.show(); for (var i in errors) { this.$errorList.append('
' + errors[i] + '
'); } } return ! errors.length }, submit: function(e){ e.preventDefault() this.$errors.hide(); this.$errorList.empty() if (! this.validate()) return var fields = this.$form.serializeArray() fields.forEach(function(pair){ if (pair.name == "password" && pair.value.length > 0) { pair.value = SHA1.hex('lol$' + pair.value + '$vvalls') } }) var request = $.post(this.action, $.param(fields)); request.done($.proxy(function (response) { if (response.error) { this.$errors.show(); for (var key in response.error.errors) { this.$errorList.append('
' + response.error.errors[key].message + '
'); } return; } else { window.location.href = "/profile" } }, this)); } })