forked from kriasoft/react-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update routing, implement Flux architecture. Closes kriasoft#14
- Loading branch information
1 parent
ddf5873
commit 1b379ea
Showing
15 changed files
with
221 additions
and
100 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
/** | ||
* @jsx React.DOM | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var {Routes, Route} = require('react-router'); | ||
var {Router} = require('director'); | ||
var AppDispatcher = require('./AppDispatcher'); | ||
var ActionTypes = require('./constants/ActionTypes'); | ||
|
||
// Export React so the dev tools can find it | ||
(window !== window.top ? window.top : window).React = React; | ||
|
||
React.renderComponent( | ||
<Routes location="history"> | ||
<Route name="app" path="/" handler={require('./layouts/Default.jsx')}> | ||
<Route name="home" path="/" handler={require('./pages/Home.jsx')} /> | ||
<Route name="privacy" handler={require('./pages/Privacy.jsx')} /> | ||
</Route> | ||
</Routes>, | ||
document.body | ||
); | ||
function render(component) { | ||
React.renderComponent(component(), document.body); | ||
} | ||
|
||
var routes = { | ||
'/': () => render(require('./pages/Home.jsx')), | ||
'/privacy': () => render(require('./pages/Privacy.jsx')) | ||
}; | ||
|
||
var router = new Router(routes).configure({html5history: true}).init(); | ||
|
||
AppDispatcher.register(function(payload) { | ||
|
||
var action = payload.action; | ||
|
||
switch (action.actionType) { | ||
case ActionTypes.SET_CURRENT_ROUTE: | ||
router.setRoute(action.route); | ||
break; | ||
} | ||
|
||
return true; // No errors. Needed by promise in Dispatcher. | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* A singleton that operates as the central hub for application updates. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var {Dispatcher} = require('flux'); | ||
var PayloadSources = require('./constants/PayloadSources'); | ||
var copyProperties = require('react/lib/copyProperties'); | ||
|
||
var AppDispatcher = copyProperties(new Dispatcher(), { | ||
|
||
/** | ||
* @param {object} action The details of the action, including the action's | ||
* type and additional data coming from the server. | ||
*/ | ||
handleServerAction: function(action) { | ||
var payload = { | ||
source: PayloadSources.SERVER_ACTION, | ||
action: action | ||
}; | ||
this.dispatch(payload); | ||
}, | ||
|
||
/** | ||
* @param {object} action The details of the action, including the action's | ||
* type and additional data coming from the view. | ||
*/ | ||
handleViewAction: function(action) { | ||
var payload = { | ||
source: PayloadSources.VIEW_ACTION, | ||
action: action | ||
}; | ||
this.dispatch(payload); | ||
} | ||
|
||
}); | ||
|
||
module.exports = AppDispatcher; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
var AppDispatcher = require('../AppDispatcher'); | ||
var ActionTypes = require('../constants/ActionTypes'); | ||
|
||
var AppActions = { | ||
|
||
/** | ||
* Set the current route. | ||
* @param {string} route Supply a route value, such as `todos/completed`. | ||
*/ | ||
setRoute: function(route) { | ||
AppDispatcher.handleViewAction({ | ||
actionType: ActionTypes.SET_CURRENT_ROUTE, | ||
route: route | ||
}); | ||
} | ||
|
||
}; | ||
|
||
module.exports = AppActions; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @jsx React.DOM | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var React = require('react'); | ||
var RouteActions = require('../actions/RouteActions'); | ||
|
||
var Link = React.createClass({ | ||
propTypes: { | ||
to: React.PropTypes.string.isRequired, | ||
children: React.PropTypes.component.isRequired | ||
}, | ||
render() { | ||
this.props.href = | ||
this.props.to && this.props.to.lastIndexOf('/', 0) === 0 ? | ||
this.props.to : '/' + this.props.to; | ||
|
||
return this.transferPropsTo( | ||
<a onClick={this.handleClick}>{this.props.children}</a> | ||
); | ||
}, | ||
handleClick(e) { | ||
e.preventDefault(); | ||
RouteActions.setRoute(this.props.to); | ||
} | ||
}); | ||
|
||
module.exports = Link; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
|
||
// Route action types | ||
SET_CURRENT_ROUTE: 'SET_CURRENT_ROUTE' | ||
|
||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
VIEW_ACTION: 'VIEW_ACTION', | ||
SERVER_ACTION: 'SERVER_ACTION' | ||
}; |
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
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
Oops, something went wrong.