Don't like ads? PRO users don't see any ads ;-)

Untitled

By: LammensJ on Jun 15th, 2012  |  syntax: JavaScript  |  size: 2.98 KB  |  hits: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. e:
  2. var e = _.extend({}, Backbone.Events);
  3. e.on('user:loggedIn', function() {
  4.         ws.navigate('/dashboard', true);
  5. });
  6.  
  7. Router:
  8. var Workspace = Backbone.Router.extend({
  9.    
  10.     _e: null,
  11.     _userStatus: null,
  12.  
  13.         routes: {
  14.                 "":"login",
  15.                 "dashboard":"dashboard",
  16.         },
  17.  
  18.         initialize: function(e) {
  19.             this._e = e;
  20.             this._userStatus = new UserStatus({e: e});
  21.            
  22.                 $('.back').live('tap', function(e) {
  23.                         window.history.back();
  24.                         return false;
  25.                 });
  26.         },
  27.  
  28.         login: function() {
  29.                 console.log('LoginPage');              
  30.                 var userLoginView = new UserLoginView({userStatus: this._userStatus});
  31.                 userLoginView.render();
  32.                 var transition = $.mobile.defaultPageTransition;
  33.                 $.mobile.changePage($(userLoginView.el), {changeHash:false, transition: transition});
  34.         },
  35.  
  36.         dashboard: function() {
  37.                 console.log('DashboardPage');
  38.                 var dashboardView = new DashboardView();
  39.                 dashboardView.render();
  40.                 var transition = $.mobile.defaultPageTransition;
  41.         $.mobile.changePage($(dashboardView.el), {changeHash:false, transition: transition});
  42.         },
  43. });
  44.  
  45. Model UserStatus:
  46. window.UserStatus = Backbone.Model.extend({
  47.    
  48.     _e: null,
  49.  
  50.         defaults: {
  51.                 loggedIn: false,
  52.                 token: null
  53.         },
  54.  
  55.         initialize: function(e) {
  56.             console.log('I\'m initialized');
  57.             this._e = e;
  58.             _.bindAll(this, 'onTokenChange');
  59.         this.on('change:token', this.onTokenChange, this);
  60.                 this.set({'token': localStorage.getItem('token')});
  61.         },
  62.  
  63.         onTokenChange: function(status, token) {
  64.             console.log('this is the token: ' + token);
  65.                 this.set({'loggedIn': !!token});
  66.                
  67.                 if(this.get('loggedIn')) {
  68.                     //console.log(this._self.get('loggedIn'));
  69.                     this._e.trigger('user:loggedIn');
  70.                 }
  71.         },
  72.  
  73.         setToken: function(token) {
  74.                 localStorage.setItem('token', token);
  75.                 this.set({'token': token});
  76.         }
  77. });
  78.  
  79. UserLoginView:
  80. window.UserLoginView = Backbone.View.extend({
  81.  
  82.         _appId: 239865876123221,
  83.         _userStatus: null,
  84.  
  85.         initialize: function(userStatus) {
  86.             console.log('Initializing Login View');
  87.             _.bindAll(this, 'render', 'onPageHide', 'onLogin');
  88.             this._userStatus = userStatus;
  89.         },
  90.  
  91.         events: {
  92.                 'pagehide':'onPageHide',
  93.                 'tap #fb-login': 'onLogin'
  94.         },
  95.  
  96.         render: function() {
  97.                 $(this.el).html(this.template);
  98.                 $(this.el).attr('data-role', 'page');
  99.                 $('body').append($(this.el));
  100.                 $(this.el).page();
  101.                
  102.                 return this;
  103.         },
  104.  
  105.         onPageHide: function() {
  106.                 $(this.el).remove();
  107.         },
  108.  
  109.         onLogin: function() {
  110.                 window.plugins.facebook.authorize(this._appId, function(res) {
  111.                         if(res.token !== undefined) {
  112.                                 // we have a token, save it (user has authenticated before)
  113.                             this._userStatus.setToken(res.token);
  114.                         } else {
  115.                                 // we have to call authorize
  116.                                 window.plugins.facebook.getAccess(function(res) {
  117.                                         if(res.token !== undefined) {
  118.                                                 // We got a token (user has authenticated just in a moment)
  119.                                                 console.log('User has authenticated just in a moment -> ' + res.token);
  120.                                                 this._userStatus.setToken(res.token);
  121.                                         }
  122.                                 });
  123.                         }
  124.                 });
  125.         }
  126. });