Created
July 20, 2016 16:00
-
-
Save methodik/0b05fbfed6faef65dbfd54cb8a8b07fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$.ajaxSetup({crossDomain : true}); | |
var suContainer = $('.ws-signup'), | |
suSubmitBtn = suContainer.find('.ws-signup-button'), | |
suInput = suContainer.find('.ws-signup-input'), | |
url = 'http://api.hevnly.com/newsletter/subscribers'; | |
console.log(suContainer); | |
var handleSuccess = function(resp) { | |
suContainer.addClass('-success').removeClass('-loading'); | |
}; | |
var handleError = function(resp) { | |
suContainer.addClass('-error').removeClass('-loading'); | |
}; | |
var resetForm = function() { | |
// remove any possible state class | |
suContainer.removeClass('-loading -success -error -overlay'); | |
// reset value of input | |
suInput.val(''); | |
} | |
var submitSubscription = function() { | |
// show overlay and loading state | |
suContainer.addClass('-overlay -loading'); | |
$.ajaxSetup({crossDomain : true}); | |
var emailRE = /.+\@.+\..+/; | |
var email = suInput.val(); | |
var isEmailValid = emailRE.test(email); | |
if (!email || !isEmailValid) { return false; } | |
var willSubmit = $.post(url, {email: email}); | |
willSubmit.done(handleSuccess); | |
willSubmit.fail(handleError); | |
}; | |
suSubmitBtn.click(function(e) { | |
submitSubscription(e); | |
}); | |
suInput.on('keyup', function(e) { | |
if (e && e.keyCode === 13) { | |
submitSubscription(e); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment