Created
July 8, 2014 14:40
-
-
Save slorber/913ece9fd7d15234a569 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
'use strict'; | |
var React = require("react/addons"); | |
var Q = require("q"); | |
var Main = require("components/layout/main"); | |
var CurrentUserService = require("services/currentUserService"); | |
var ApiRequestSessionHolder = require("repositories/utils/apiRequestSessionHolder"); | |
var Preconditions = require("common/preconditions"); | |
var DeepFreeze = require("common/deepFreeze"); | |
var SelectedSpaceModel = require("model/selectedSpaceModel"); | |
var app = { | |
currentAppState: undefined, | |
initialize: function() { | |
Q.longStackSupport = true; | |
// TODO in next versions of React it was said to be on by default, so probably deprecated | |
React.initializeTouchEvents(true); | |
document.addEventListener('deviceready', this.onDeviceReady.bind(this) , false); | |
}, | |
onDeviceReady: function() { | |
var self = this; | |
console.debug("Device ready, will try to auto log in and mount React app"); | |
// TODO do something before waiting for the autologin to success! | |
CurrentUserService.autoLogin() | |
.then(function(currentUser) { | |
console.debug("User auto login success",currentUser); | |
self.setCurrentAppStateAndRender( | |
self.buildInitialAppState(currentUser) | |
); | |
}) | |
.fail(function(error) { | |
console.warn("Unable to auto login. The app state will be initialised in an unauthenticated state",error); | |
self.setCurrentAppStateAndRender( | |
self.buildInitialAppState(undefined) | |
); | |
}) | |
.done(); | |
}, | |
buildInitialAppState: function(currentUser) { | |
return { | |
selectedPane: "Stamples", | |
currentUser: currentUser, // The user may be undefined here on startup! | |
selectedSpace: SelectedSpaceModel.forAll() | |
}; | |
}, | |
buildAppProps: function(newAppState) { | |
var self = this; | |
return { | |
appState: newAppState, | |
updateAppState: self.setCurrentAppStateAndRender.bind(self) | |
}; | |
}, | |
setCurrentAppStateAndRender: function(newAppState) { | |
this.setCurrentAppState(newAppState); | |
this.renderCurrentAppState(); | |
}, | |
setCurrentAppState: function(newAppState) { | |
Preconditions.checkMandatoryParameter(newAppState,"newAppState is required"); | |
this.currentAppState = newAppState; | |
DeepFreeze(this.currentAppState); | |
}, | |
renderCurrentAppState: function() { | |
if ( this.currentAppState.currentUser ) { | |
// This is required to automatically perform authenticated requests | |
// without having to configure the request headers every time... | |
ApiRequestSessionHolder.setApiRequestSession(this.currentAppState.currentUser.sessionToken); | |
} | |
if ( !this.appComponent ) { | |
var mountNode = document.getElementById('reactAppContainer'); | |
var mountComponent = Main( this.buildAppProps(this.currentAppState) ); | |
console.debug("Rendering main component",this.currentAppState); | |
this.appComponent = React.renderComponent(mountComponent,mountNode); | |
} | |
else { | |
console.debug("Re-rendering main component",this.currentAppState); | |
this.appComponent.setProps( this.buildAppProps(this.currentAppState) ); | |
} | |
} | |
}; | |
app.initialize(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment