Skip to content

Instantly share code, notes, and snippets.

@jhubert
Last active August 29, 2015 14:10
Show Gist options
  • Save jhubert/783e130239b4d55c43b7 to your computer and use it in GitHub Desktop.
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. :)
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
React = require 'react'
{div, p} = React.DOM
module.exports = React.createClass
displayName: 'IntroPage'
render: ->
div {},
p {}, 'Welcome to my ReactJS app'
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.
module.exports = (ctx, next) ->
console.log 'Middlware Example', ctx
next();
React = require 'react'
{div, p} = React.DOM
module.exports = React.createClass
displayName: 'NotFoundPage'
render: ->
div {},
p {}, 'Page Not Found'
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
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