Last active
August 29, 2015 14:15
-
-
Save chochinlu/b5ae4fa7a0e7fd0eb007 to your computer and use it in GitHub Desktop.
Fluxible router example (1) source: http://fluxible.io/tutorials/routing.html
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 React = require('react'); | |
var Fluxible = require('fluxible'); | |
var routrPlugin = require('fluxible-plugin-routr'); | |
var app = new Fluxible({ | |
appComponent: React.createFactory(require('./components/Application.jsx')) | |
}); | |
app.plug(routrPlugin({ | |
routes: require('./configs/routes') | |
})); | |
//Passthrough to dispatchr's registerStore function | |
app.registerStore(require('./stores/ApplicationStore')); | |
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
var React = require('react'); | |
var FluxibleMixin = require('fluxible').Mixin; | |
var ApplicationStore = require('../stores/ApplicationStore'); | |
var Home = require('./Home.jsx'); | |
var About = require('./About.jsx'); | |
var Application = React.createClass({ | |
mixins: [FluxibleMixin], | |
getInitialState: function(){ | |
return this.getStore(ApplicationStore).getState(); | |
}, | |
render: function(){ | |
var output = ''; | |
switch (this.state.currentPageName) { | |
case 'home': | |
output = <Home />; | |
break; | |
case 'about': | |
output = <About />; | |
break; | |
} | |
return ( | |
<div>{output}</div> | |
); | |
} | |
}); | |
module.exports = Application; |
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 createStore = require('fluxible/utils/createStore'); | |
var routesConfig = require('../configs/routes'); | |
var ApplicationStore = createStore({ | |
storeName: 'ApplicationStore', | |
initialize: function(dispatcher){ //dispacher parameter? | |
this.currentPageName = null; | |
this.currentPage = null; | |
this.currentRoute = null; | |
this.pages = routesConfig; | |
}, | |
handlers: { | |
'CHANGE_ROUTE_SUCCESS': 'handleNavigate' | |
}, | |
handleNavigate: function(route){ | |
if (this.currentRoute && (this.currentRoute.url === route.url)) { | |
return; | |
} | |
var pageName = route.config.page; | |
var page = this.pages[pageName]; | |
this.currentPageName = pageName; | |
this.currentPage = page; | |
this.currentRoute = route; | |
this.emitChange(); | |
}, | |
getCurrentPageName: function () { | |
return this.currentPageName; | |
}, | |
getState: function () { | |
return { | |
currentPageName: this.currentPageName, | |
currentPage: this.currentPage, | |
pages: this.pages, | |
route: this.currentRoute, | |
}; | |
} | |
}); | |
module.exports = ApplicationStore; |
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 React = require('react'); | |
var Home = React.createClass({ | |
render: function(){ | |
return ( | |
<div>Home</div> | |
); | |
} | |
}); | |
module.exports = Home; |
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
{ | |
"name": "fluxible-router-1", | |
"scripts": { | |
"start": "DEBUG=server node server" | |
}, | |
"dependencies": { | |
"debug": "^2.1.1", | |
"express": "^4.11.2", | |
"fluxible": "^0.2.1", | |
"fluxible-plugin-routr": "^0.3.0", | |
"node-jsx": "^0.12.4", | |
"react": "^0.12.2" | |
} | |
} |
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 = { | |
home: { | |
path: '/', | |
method: 'get', | |
label: 'Home', | |
page: 'home' | |
}, | |
about: { | |
path: '/about', | |
method: 'get', | |
label: 'About', | |
page: 'about' | |
} | |
}; |
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
require('node-jsx').install({extension: '.jsx'}); | |
var debug = require('debug')('server'); | |
var React = require('react'); | |
var navigateAction = require('flux-router-component').navigateAction; | |
var app = require('./app'); | |
var express = require('express'); | |
var server = express(); | |
server.use(function(req, res, next){ | |
debug('createContext'); | |
var context = app.createContext(); | |
var params = {url: req.url}; | |
debug('Executing navigate action'); | |
context.executeAction(navigateAction, params, function(err){ | |
if (err) { | |
if (err.status && err.status === 404) { | |
next(); | |
} else { | |
next(err); | |
} | |
return; | |
} | |
debug('rendering application component into html'); | |
var AppComponent = app.getAppComponent(); //factory function | |
React.withContext(context.getComponentContext(), function(){ | |
var html = React.renderToString(AppComponent()); | |
debug('sending markup'); | |
res.write(html); | |
res.end(); | |
}); | |
}); | |
}); | |
var port = process.env.PORT || 3000; | |
server.listen(port); | |
console.log('Listening on port ' + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment