diff options
Diffstat (limited to 'public/assets/javascripts/ui/SignUpModal.js')
| -rw-r--r-- | public/assets/javascripts/ui/SignUpModal.js | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/public/assets/javascripts/ui/SignUpModal.js b/public/assets/javascripts/ui/SignUpModal.js new file mode 100644 index 0000000..160323b --- /dev/null +++ b/public/assets/javascripts/ui/SignUpModal.js @@ -0,0 +1,89 @@ + + +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('<div>' + errors[i] + '</div>'); + } + } + + 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('<div>' + response.error.errors[key].message + '</div>'); + } + return; + } + else { + window.location.href = "/profile" + } + }, this)); + } + +}) |
