Last active
September 27, 2015 11:16
-
-
Save auser/b78b14c011a639d0799e 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
import React from 'react-native'; | |
import Routes from '../routes'; | |
export class App extends React.Component { | |
componentDidMount() { | |
// ... bootstrap goes here | |
} | |
_renderScene(route, nav) { | |
let page; | |
if (!this.state.bootstrapped) page = this._getLoadingPage(); | |
else if (!this.state.authenticated) page = this._getLoginPage(); | |
else page = this._getPage(route, nav); | |
let Component = page.Component; | |
let componentProps = { | |
navigator: nav, | |
route: route, | |
page: page, | |
Routes: routes | |
} | |
return (<View style={styles.navigator}> | |
<Component {...componentProps} /> | |
</View>) | |
} | |
_getLoginPage() { | |
return Routes.getRoute('login.show', {}); | |
} | |
_getPage(route, nav) { | |
return Routes.getRoute(route.id); | |
} | |
// ... example for the user | |
render() { | |
if (!this.state.bootstrapped) return <Loading /> | |
return ( | |
<Navigator | |
initialRoute={this.state.initialRoute} | |
renderScene={this._renderScene.bind(this)} | |
configureScene={this._configureScene.bind(this)} | |
/> | |
) | |
} | |
} |
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
import React from 'react-native' | |
class SearchPage extends React.Component { | |
render() { | |
return <View... />; | |
} | |
} | |
export const Routes = { | |
'search.index': { | |
Component: SearchPage, | |
title: 'search' | |
} | |
} |
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
var _ = require('lodash'); | |
class Routes { | |
constructor() { | |
this.routes = {}; | |
} | |
addRoutes(Conf) { | |
let Routes = Conf.Routes ? Conf.Routes : Conf; | |
_.map(Routes, (route, name) => { | |
route.id = route.id || name; | |
return this.routes[route.id] = route; | |
}); | |
} | |
getRoute(name_or_id, hsh) { | |
if (!hsh) hsh = {} | |
var route = this.routes[name_or_id]; | |
if (route) { | |
route = _.extend({}, route, hsh); | |
return route; | |
} | |
} | |
} | |
import Login from './pages/Login'; | |
import * as Profile from './pages/Profile'; | |
import * as Search from './pages/Search'; | |
var routes = new Routes(); | |
routes.addRoutes(Login); | |
routes.addRoutes(Profile.Routes); | |
routes.addRoutes(Search.Routes); | |
module.exports = routes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment