Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- })
- })
- });
Add Comment
Please, Sign In to add comment