Last active
August 29, 2015 14:10
-
-
Save jhubert/783e130239b4d55c43b7 to your computer and use it in GitHub Desktop.
A quick sample of the ReactJS / pagejs combination I've been using in my apps. So far so good. Comments appreciated. :)
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
React = require 'react' | |
Router = require 'router' | |
routes = | |
middleware: [ | |
["*", require('./middleware-example')] | |
] | |
pages:[ | |
["/", require('./Intro'), 'intro'] | |
["*", require('./NotFound'), 'not-found'] | |
] | |
app = | |
start: (target) -> | |
React.renderComponent new Router(routes: routes), target || document.querySelector("body") | |
module.exports = app |
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
React = require 'react' | |
{div, p} = React.DOM | |
module.exports = React.createClass | |
displayName: 'IntroPage' | |
render: -> | |
div {}, | |
p {}, 'Welcome to my ReactJS app' |
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
The MIT License (MIT) | |
Copyright (c) 2014 Jeremy Baker | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
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
module.exports = (ctx, next) -> | |
console.log 'Middlware Example', ctx | |
next(); |
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
React = require 'react' | |
{div, p} = React.DOM | |
module.exports = React.createClass | |
displayName: 'NotFoundPage' | |
render: -> | |
div {}, | |
p {}, 'Page Not Found' |
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
React = require 'react' | |
page = require 'page' | |
ErrorScreen = React.createClass | |
displayName: 'ErrorScreen' | |
render: -> | |
React.DOM.div({}, 'There was an issue loading the page. Please try again.') | |
addMiddleware = (route) -> | |
page route[0], route[1] | |
return | |
addPage = (route) -> | |
url = route[0] | |
Component = route[1] | |
page url, (ctx) => | |
# Set the body class based on the current page | |
document.querySelector('body').className = ['body', route[2]].filter(Boolean).join('-') | |
# Scroll the window to the top each time a page gets shown | |
window.scrollTo(0, 0) | |
@setState | |
component: Component | |
params: ctx.params | |
querystring: ctx.querystring | |
context: ctx | |
return | |
return | |
Router = React.createClass | |
displayName: 'Router' | |
propTypes: | |
routes: React.PropTypes.shape | |
pages: React.PropTypes.array.isRequired | |
middleware: React.PropTypes.array.isRequired | |
getDefaultProps: -> | |
initialComponent: ErrorScreen | |
componentWillMount: -> | |
@props.routes.middleware.forEach addMiddleware.bind(this) | |
@props.routes.pages.forEach addPage.bind(this) | |
componentDidMount: -> | |
page.start() | |
getInitialState: -> | |
component: @props.initialComponent | |
params: {} | |
querystring: null | |
context: {} | |
render: -> | |
new @state.component | |
params: @state.params | |
querystring: @state.querystring | |
context: @state.context |
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
app = require 'app' | |
# Anything else you need to do in order to prep the app can happen here. | |
module.exports = app.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment