Guest User

Untitled

a guest
Jun 24th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. User = Em.Object.extend({
  2.   firstname: null,
  3.   lastname: null,
  4.   email: null,
  5.   active: null,
  6.   fullName: function(){
  7.     return this.get('firstname') + " " + this.get('lastname')
  8.   }.property('firstname', 'lastname').cacheable()
  9. })
  10.  
  11. Auth = Em.Namespace.create({
  12.   loadingAnimation : "static/img/ajax-loader.gif" // some way to make this app specific?
  13.   // user is prepopulated by the app as a global object
  14.   user: User.create(user), // some better way to do this? A cookie perhaps?
  15. });
  16.  
  17.  
  18. if ('undefined' !== typeof window) {
  19.   window.Auth = Auth
  20. }
  21.  
  22.  
  23. Auth.loggedOutView = Em.View.extend({
  24.   templateName: "login",
  25.   LoginFormView: Em.View.extend({
  26.     errorMsgBinding: 'Auth.UserFormController.content.errorMsg',
  27.     usernameBinding: 'Auth.UserFormController.content.username',
  28.     passwordBinding: 'Auth.UserFormController.content.password',
  29.     submitLogin:function(){
  30.       Auth.LoginManager.send('login', {username: this.get('username'),
  31.                                             password: this.get('password')
  32.                                            })
  33.     }
  34.   }),
  35. });
  36.  
  37.  
  38. Auth.UserFormController = Em.Object.create({
  39.   content: Em.Object.create({username: null,
  40.                              password: null,
  41.                              errorMsg: null,
  42.                             })
  43. })
  44.  
  45.  
  46. Auth.loggingInAnimation = Em.View.extend({
  47.   templateName : "login-animation",
  48. })
  49.  
  50.  
  51. Auth.LoginManager = Em.StateManager.create({
  52.   start: Em.State.create({
  53.     enter: function(manager, transition){
  54.  
  55.       if (Auth.user.get('active')){
  56.         manager.goToState('loggedInState')
  57.       } else {
  58.         transition.async()
  59.         transition.resume()
  60.         manager.goToState('loggedOutState')
  61.       }
  62.     }
  63.   }),
  64.  
  65.   loggedOutState : Em.ViewState.create({
  66.     view: Auth.loggedOutView.create(),
  67.  
  68.     loggingInState : Em.ViewState.create({
  69.       view: Auth.loggingInAnimation.create(),
  70.     }),
  71.  
  72.     loginErrState : Em.State.create({
  73.       enter: function(manager, transition){
  74.         content = ClientHub.UserFormController.get('content')
  75.         content.set('errorMsg', "There was a problem logging you in. Please try again.")
  76.       },
  77.       exit: function(manager, transition){
  78.         content = ClientHub.UserFormController.get('content')
  79.         content.set('errorMsg', null)
  80.       },
  81.     }),
  82.  
  83.     login: function(manager, usernamePass){
  84.       //send xhr request
  85.       $.post("/auth/authentication/", usernamePass)
  86.         .success(function(data){
  87.           manager.send('loginSuccessful', data)
  88.         })
  89.         .error(function(data){
  90.           manager.send('loginError', data)
  91.         })
  92.       manager.goToState('loggedOutState.loggingInState')
  93.     },
  94.     loginSuccessful: function(manager, data){
  95.       Auth.user = User.create(data)
  96.       manager.goToState('loggedInState')
  97.     },
  98.     loginError: function(manager, data){
  99.       manager.goToState('loggedOutState.loginErrState')
  100.     }
  101.  
  102.   }),
  103.  
  104.   loggedInState : Em.State.create({
  105.     //view: Auth.loggedInView.create()
  106.   }),
  107.  
  108. });
Add Comment
Please, Sign In to add comment