Created
May 30, 2014 13:06
-
-
Save nehalist/bf9cb17be915d2802ac5 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
$(function() { | |
var Login = { | |
loginButton: $('#login-button'), | |
loginButtonContent: $('#login-button').html(), | |
errorContainer: $('#error-container'), | |
loadingSpinner: $('#loading-spinner'), | |
showErrorContainer: function(message) { | |
this.errorContainer.show(); | |
this.errorContainer.html(message); | |
}, | |
hideErrorContainer: function() { | |
this.errorContainer.hide(); | |
}, | |
enableLoginButton: function() { | |
this.loginButton.removeAttr('disabled'); | |
this.loginButton.html(this.loginButtonContent); | |
}, | |
disableLoginButton: function() { | |
this.loginButton.attr('disabled', 'disabled'); | |
this.loginButton.html('Authenticating...'); | |
}, | |
toggleLoadingSpinner: function() { | |
this.loadingSpinner.toggle(); | |
}, | |
validateInput: function(data) { | |
var regex = /\S+@\S+\.\S+/; | |
var mail = regex.test(data.email); | |
var pwd = (data.password.length > 0); | |
if(mail && pwd) { | |
return true; | |
} | |
return false; | |
}, | |
procceed: function(data) { | |
if(this.validateInput(data)) { | |
var self = this; | |
$.ajax({ | |
type: 'POST', | |
url: login_url, | |
data: { | |
email: data.email, | |
password: data.password, | |
remember: data.remember | |
}, | |
success: function(data) { | |
console.log(data); | |
}, | |
error: function(xhr, textStatus, thrownError) { | |
console.log('error'); | |
}, | |
beforeSend: function() { | |
self.hideErrorContainer(); | |
self.disableLoginButton(); | |
self.toggleLoadingSpinner(); | |
} | |
}); | |
} else { | |
this.showErrorContainer('Invalid input'); | |
} | |
} | |
} | |
$('#login-button').click(function(e) { | |
e.preventDefault(); | |
Login.procceed({ | |
email: $('#usermail').val(), | |
password: $('#password').val(), | |
remember: $('#remember').val() | |
}); | |
}); | |
}); |
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
jQuery($) -> | |
class Login | |
constructor: -> | |
@loginButton = $('#login-button') | |
@loginButtonContent = @loginButton.html() | |
@errorContainer = $('#error-container') | |
showErrorContainer: (message) -> | |
@errorContainer.show() | |
@errorContainer.html(message) | |
return | |
hideErrorContainer: -> | |
@errorContainer.hide() | |
return | |
enableLoginButton: -> | |
@loginButton.removeAttr('disabled') | |
@loginButton.html('Whoop') | |
return | |
disableLoginButton: -> | |
@loginButton.attr('disabled', 'disabled') | |
@loginButton.html('Disabled') | |
return | |
toggleLoadingSpinner: -> | |
@loadingSpinner.toggle() | |
return | |
validateInput: (input) -> | |
regex = /\S+@\S+\.\S+/ | |
mail = regex.test(input.email) | |
pwd = true if (input.password.length > 0) | |
if mail and pwd | |
return true | |
return false | |
procceed: (data) -> | |
if(@validateInput(data)) | |
self = @ | |
$.ajax @loginURL, | |
type: 'POST' | |
data: { | |
email: data.email, | |
password: data.password, | |
remember: data.remeber | |
} | |
success: (data) -> | |
console.log(data) | |
error: (xhr, textStatus, thrownError) -> | |
console.log('error') | |
beforeSend: -> | |
self.hideErrorContainer | |
self.disableLoignButton | |
self.toggleLoadingSpinner | |
return | |
else | |
@showErrorContainer('Invalid input') | |
$('#login-button').bind 'click', (event) => | |
event.preventDefault() | |
Login = new Login | |
Login.procceed({ | |
email: $('#usermail').val(), | |
password: $('#password').val() | |
remember: $('#remeber').val() | |
}) |
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
// Generated by CoffeeScript 1.7.1 | |
(function() { | |
jQuery($)(function() { | |
var Login; | |
Login = (function() { | |
function Login() { | |
this.loginButton = $('#login-button'); | |
this.loginButtonContent = this.loginButton.html(); | |
this.errorContainer = $('#error-container'); | |
} | |
Login.prototype.showErrorContainer = function(message) { | |
this.errorContainer.show(); | |
this.errorContainer.html(message); | |
}; | |
Login.prototype.hideErrorContainer = function() { | |
this.errorContainer.hide(); | |
}; | |
Login.prototype.enableLoginButton = function() { | |
this.loginButton.removeAttr('disabled'); | |
this.loginButton.html('Whoop'); | |
}; | |
Login.prototype.disableLoginButton = function() { | |
this.loginButton.attr('disabled', 'disabled'); | |
this.loginButton.html('Disabled'); | |
}; | |
Login.prototype.toggleLoadingSpinner = function() { | |
this.loadingSpinner.toggle(); | |
}; | |
Login.prototype.validateInput = function(input) { | |
var mail, pwd, regex; | |
regex = /\S+@\S+\.\S+/; | |
mail = regex.test(input.email); | |
if (input.password.length > 0) { | |
pwd = true; | |
} | |
if (mail && pwd) { | |
return true; | |
} | |
return false; | |
}; | |
Login.prototype.procceed = function(data) { | |
var self; | |
if (this.validateInput(data)) { | |
self = this; | |
return $.ajax(this.loginURL, { | |
type: 'POST', | |
data: { | |
email: data.email, | |
password: data.password, | |
remember: data.remeber | |
}, | |
success: function(data) { | |
return console.log(data); | |
}, | |
error: function(xhr, textStatus, thrownError) { | |
return console.log('error'); | |
}, | |
beforeSend: function() { | |
self.hideErrorContainer; | |
self.disableLoignButton; | |
return self.toggleLoadingSpinner; | |
} | |
}); | |
} else { | |
return this.showErrorContainer('Invalid input'); | |
} | |
}; | |
return Login; | |
})(); | |
return $('#login-button').bind('click', (function(_this) { | |
return function(event) { | |
event.preventDefault(); | |
Login = new Login; | |
return Login.procceed({ | |
email: $('#usermail').val(), | |
password: $('#password').val(), | |
remember: $('#remeber').val() | |
}); | |
}; | |
})(this)); | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test