Skip to content

Instantly share code, notes, and snippets.

Created July 14, 2011 12:59
Show Gist options
  • Save anonymous/1082401 to your computer and use it in GitHub Desktop.
Save anonymous/1082401 to your computer and use it in GitHub Desktop.
My.coreStateChart = SC.Statechart.create({
rootState: SC.State.design({
initialSubstate: 'checkingState',
checkingState: SC.State.design({
enterState: function(){
return SC.Async.perform('isLoggedInCheck');
},
exitState: function() {
},
isLoggedInCheck: function() {
SC.Request.getUrl('/api/user/is_logged').notify(this, 'isLoggedInResponse').header({ 'Accept': 'applicaton/json' }).send();
},
isLoggedInResponse: function(response) {
this.resumeGotoState();
if (SC.ok(response)) {
var response = SC.json.decode(response.encodedBody());
if (response.status == 'OK') {
this.gotoState('mainState');
}
else {
this.gotoState('displayState');
}
}
}
}),
displayState: SC.State.design({
enterState: function() {
My.getPath('LoginPage').append();
},
exitState: function() {
My.getPath('LoginPage').remove();
},
loginClicked: function() {
return SC.Async.perform('loginCheck');
},
loginCheck: function() {
if (My.loginController.username == '' || My.loginController.password == '') My.displayController.showError('Username or password field empty.', 'Please try again.');
else
if (!isRFC822ValidEmail(this.username)) My.displayController.showError('Email address not valid.', 'Please try again.');
else {
var tmp = sha1(this.password);
var object = { username: this.username, password: tmp };
SC.Request.postUrl('/api/user/login').notify(this, 'loginResponse').header({ 'Accept': 'applicaton/json' }).json().send(object);
}
},
loginResponse: function(response) {
if (SC.ok(response)) {
var response = SC.json.decode(response.encodedBody());
this.resumeGotoState();
if (response.status == 'OK') {
gotoState('mainState');
}
else {
My.displayController.showError("Wrong username or password.", "Please try again.");
this.gotoState('displayState');
}
}
}
}),
mainState: SC.State.design({
enterState: function() {
My.getPath('MainPage').append();
},
exitState: function() {
My.getPath('MainPage').remove();
}
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment