Skip to content

Commit 53f942b

Browse files
Michael Ridgwaymridgway
Michael Ridgway
authored andcommitted
[WIP] Fluxible Router
1 parent 36e7727 commit 53f942b

File tree

9 files changed

+34
-96
lines changed

9 files changed

+34
-96
lines changed

Gruntfile.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var webpack = require('webpack');
2+
13
module.exports = function (grunt) {
24
grunt.initConfig({
35
clean: ['./' + grunt.option('taskName') + '/build'],
@@ -44,9 +46,14 @@ module.exports = function (grunt) {
4446
module: {
4547
loaders: [
4648
{ test: /\.css$/, loader: 'style!css' },
47-
{ test: /\.(js|jsx)$/, exclude: /node_modules/, loader: 'babel-loader' }
49+
{ test: /\.(js|jsx)$/, exclude: /node_modules/, loader: require.resolve('babel-loader') }
4850
]
4951
},
52+
plugins: [
53+
// Protects against multiple React installs when npm linking
54+
new webpack.NormalModuleReplacementPlugin(/^react?$/, require.resolve('react')),
55+
new webpack.NormalModuleReplacementPlugin(/^react(\/addons)?$/, require.resolve('react/addons'))
56+
],
5057
stats: {
5158
colors: true
5259
},

fluxible-router/app.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
'use strict';
66
var React = require('react');
77
var Fluxible = require('fluxible');
8-
var routrPlugin = require('fluxible-plugin-routr');
98

109
var app = new Fluxible({
1110
component: React.createFactory(require('./components/Application.jsx'))
1211
});
1312

14-
app.plug(routrPlugin({
15-
routes: require('./configs/routes')
16-
}));
17-
13+
app.registerStore(require('./stores/RouteStore'));
1814
app.registerStore(require('./stores/ApplicationStore'));
1915
app.registerStore(require('./stores/TimeStore'));
2016
app.registerStore(require('./stores/PageStore'));

fluxible-router/components/Application.jsx

+11-24
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var About = require('./About.jsx');
1010
var Page = require('./Page.jsx');
1111
var Timestamp = require('./Timestamp.jsx');
1212
var ApplicationStore = require('../stores/ApplicationStore');
13-
var RouterMixin = require('flux-router-component').RouterMixin;
1413
var FluxibleMixin = require('fluxible').FluxibleMixin;
14+
var RouterMixin = require('fluxible-router').RouterMixin;
1515

1616
var Application = React.createClass({
1717
mixins: [RouterMixin, FluxibleMixin],
@@ -25,36 +25,23 @@ var Application = React.createClass({
2525
var state = this.getStore(ApplicationStore).getState();
2626
this.setState(state);
2727
},
28-
render: function () {
29-
var output = '';
30-
//choose the right page based on the route
31-
switch (this.state.currentPageName) {
32-
case 'home':
33-
output = <Home/>;
34-
break;
35-
case 'about':
36-
output = <About/>;
37-
break;
38-
case 'page':
39-
output = <Page context={this.props.context}/>;
40-
break;
28+
componentDidUpdate: function(prevProps, prevState) {
29+
var newState = this.state;
30+
if (newState.pageTitle === prevState.pageTitle) {
31+
return;
4132
}
33+
document.title = newState.pageTitle;
34+
},
35+
render: function () {
36+
var Handler = this.state.currentRoute.get('handler');
4237
//render content
4338
return (
4439
<div>
45-
<Nav selected={this.state.currentPageName} links={this.state.pages} />
46-
{output}
40+
<Nav />
41+
<Handler />
4742
<Timestamp />
4843
</div>
4944
);
50-
},
51-
52-
componentDidUpdate: function(prevProps, prevState) {
53-
var newState = this.state;
54-
if (newState.pageTitle === prevState.pageTitle) {
55-
return;
56-
}
57-
document.title = newState.pageTitle;
5845
}
5946
});
6047

fluxible-router/components/Nav.jsx

+3-29
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,14 @@
44
*/
55
'use strict';
66
var React = require('react');
7-
var NavLink = require('flux-router-component').NavLink;
7+
var NavLink = require('fluxible-router').NavLink;
88

99
var Nav = React.createClass({
10-
getDefaultProps: function () {
11-
return {
12-
selected: 'home',
13-
links: {}
14-
};
15-
},
1610
render: function() {
17-
var selected = this.props.selected,
18-
links = this.props.links,
19-
context = this.props.context,
20-
linkHTML = Object.keys(links).map(function (name) {
21-
var className = '',
22-
link = links[name];
23-
24-
//print only link with label
25-
if(link.label) {
26-
if (selected === name) {
27-
className = 'pure-menu-selected';
28-
}
29-
30-
return (
31-
<li className={className} key={link.path}>
32-
<NavLink routeName={link.page}>{link.label}</NavLink>
33-
</li>
34-
);
35-
}
36-
37-
});
3811
return (
3912
<ul className="pure-menu pure-menu-open pure-menu-horizontal">
40-
{linkHTML}
13+
<li><NavLink routeName="home" activeStyle={{backgroundColor: '#ccc'}}>Home</NavLink></li>
14+
<li><NavLink routeName="about" activeStyle={{backgroundColor: '#ccc'}}>About</NavLink></li>
4115
</ul>
4216
);
4317
}

fluxible-router/configs/routes.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
home: {
33
path: '/',
44
method: 'get',
5-
page: 'home',
5+
handler: require('../components/Home.jsx'),
66
label: 'Home',
77
action: function (context, payload, done) {
88
context.dispatch('UPDATE_PAGE_TITLE', { pageTitle: 'Home | flux-examples | routing' });
@@ -12,7 +12,7 @@ module.exports = {
1212
about: {
1313
path: '/about',
1414
method: 'get',
15-
page: 'about',
15+
handler: require('../components/About.jsx'),
1616
label: 'About',
1717
action: function (context, payload, done) {
1818
context.dispatch('UPDATE_PAGE_TITLE', { pageTitle: 'About | flux-examples | routing' });
@@ -22,7 +22,7 @@ module.exports = {
2222
dynamicpage: {
2323
path: '/page/:id',
2424
method: 'get',
25-
page: 'page',
25+
handler: require('../components/Page.jsx'),
2626
action: function (context, payload, done) {
2727
context.dispatch('LOAD_PAGE', { id: payload.params.id });
2828
context.dispatch('UPDATE_PAGE_TITLE', { pageTitle: payload.params.id + ' [Dynamic Page] | flux-examples | routing' });

fluxible-router/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require('babel/register');
77
var express = require('express');
88
var favicon = require('serve-favicon');
99
var serialize = require('serialize-javascript');
10-
var navigateAction = require('flux-router-component').navigateAction;
10+
var navigateAction = require('fluxible-router').navigateAction;
1111
var debug = require('debug')('Example');
1212
var React = require('react');
1313
var app = require('./app');

fluxible-router/stores/ApplicationStore.js

+2-32
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,31 @@
44
*/
55
'use strict';
66
var createStore = require('fluxible/addons').createStore;
7-
var routesConfig= require('../configs/routes')
87

98
var ApplicationStore = createStore({
109
storeName: 'ApplicationStore',
1110
handlers: {
12-
'CHANGE_ROUTE_SUCCESS' : 'handleNavigate',
1311
'UPDATE_PAGE_TITLE' : 'updatePageTitle'
1412
},
1513
initialize: function () {
16-
this.currentPageName = null;
17-
this.currentPage = null;
18-
this.currentRoute = null;
19-
this.pages = routesConfig;
2014
this.pageTitle = '';
2115
},
22-
handleNavigate: function (route) {
23-
if (this.currentRoute && (this.currentRoute.url === route.url)) {
24-
return;
25-
}
26-
27-
var pageName = route.config.page;
28-
var page = this.pages[pageName];
29-
30-
this.currentPageName = pageName;
31-
this.currentPage = page;
32-
this.currentRoute = route;
16+
updatePageTitle: function (payload) {
17+
this.pageTitle = payload.pageTitle;
3318
this.emitChange();
3419
},
35-
updatePageTitle: function (title) {
36-
this.pageTitle = title.pageTitle;
37-
this.emitChange();
38-
},
39-
getCurrentPageName: function () {
40-
return this.currentPageName;
41-
},
4220
getPageTitle: function () {
4321
return this.pageTitle;
4422
},
4523
getState: function () {
4624
return {
47-
currentPageName: this.currentPageName,
48-
currentPage: this.currentPage,
49-
pages: this.pages,
50-
route: this.currentRoute,
5125
pageTitle: this.pageTitle
5226
};
5327
},
5428
dehydrate: function () {
5529
return this.getState();
5630
},
5731
rehydrate: function (state) {
58-
this.currentPageName = state.currentPageName;
59-
this.currentPage = state.currentPage;
60-
this.pages = state.pages;
61-
this.currentRoute = state.route;
6232
this.pageTitle = state.pageTitle;
6333
}
6434
});

fluxible-router/stores/RouteStore.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var RouteStore = require('fluxible-router').RouteStore;
2+
var routes = require('../configs/routes');
3+
4+
module.exports = RouteStore.withStaticRoutes(routes);

fluxible-router/stores/TimeStore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var TimeStore = createStore({
1616
this.emitChange();
1717
},
1818
handlers: {
19-
'CHANGE_ROUTE_START': 'handleTimeChange',
19+
'NAVIGATE_START': 'handleTimeChange',
2020
'UPDATE_TIME': 'handleTimeChange'
2121
},
2222
getState: function () {

0 commit comments

Comments
 (0)