Skip to content

Commit 3b209fc

Browse files
author
likun7981
committed
system.import
1 parent d691352 commit 3b209fc

File tree

16 files changed

+255
-187
lines changed

16 files changed

+255
-187
lines changed

build/webpack-config/alias.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module.exports = paths => ({
2-
REDUCER_TOOL: paths.src('store/reducerTool'),
3-
FETCH: paths.src('fetch'),
2+
UTILS: paths.src('utils'),
43
COMPONENTS: paths.src('components'),
54
CONTAINERS: paths.src('containers'),
65
STYLES: paths.src('static/styles'),

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"react-router": "^3.0.0",
7373
"redux": "^3.6.0",
7474
"redux-logger": "^2.7.4",
75-
"redux-thunk": "^2.1.0"
75+
"redux-thunk": "^2.1.0",
76+
"whatwg-fetch": "^2.0.1"
7677
}
7778
}

src/components/Header/Header.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export const Header = () => (
99
Home
1010
</IndexLink>
1111
{' · '}
12-
<Link to="/counter" activeClassName="route--active">
13-
Counter
12+
<Link to="/increase" activeClassName="route--active">
13+
Increase
1414
</Link>
1515
</div>
1616
);

src/reducers/location.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { createReducer } from 'REDUCER_TOOL';
2-
31
// ------------------------------------
42
// Constants
53
// ------------------------------------
@@ -18,11 +16,9 @@ export function locationChange(location = '/') {
1816
// ------------------------------------
1917
// Specialized Action Creator
2018
// ------------------------------------
21-
export const updateLocation = ({ dispatch }) => nextLocation => dispatch(locationChange(nextLocation));
19+
export const updateLocation = ({ dispatch }) =>
20+
nextLocation =>
21+
dispatch(locationChange(nextLocation));
2222

23-
// ------------------------------------
24-
// Reducer
25-
// ------------------------------------
26-
export default createReducer({
27-
[LOCATION_CHANGE]: (state, action) => action.payload
28-
}, null);
23+
export default (state = null, action) =>
24+
(action.type === LOCATION_CHANGE ? action.payload : state);

src/routes/Counter/containers/CounterContainer.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/routes/Counter/index.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/routes/Home/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import HomeViewComponent from './components/HomeView';
1+
import HomeComponent from './components/HomeView';
22

33
export default {
4-
component: HomeViewComponent
4+
component: HomeComponent
55
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22

3-
export const Counter = props => (
3+
export const Increase = props => (
44
<div style={{ margin: '0 auto' }} >
5-
<h2>Counter: {props.counter}</h2>
5+
<h2>Counter: {props.increase}</h2>
66
<button className="btn btn-default" onClick={props.increment}>
77
Increment
88
</button>
@@ -13,10 +13,10 @@ export const Counter = props => (
1313
</div>
1414
);
1515

16-
Counter.propTypes = {
17-
counter: React.PropTypes.number.isRequired,
16+
Increase.propTypes = {
17+
increase: React.PropTypes.number.isRequired,
1818
doubleAsync: React.PropTypes.func.isRequired,
1919
increment: React.PropTypes.func.isRequired
2020
};
2121

22-
export default Counter;
22+
export default Increase;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { connect } from 'react-redux';
2+
import { actions } from '../reducers/increase';
3+
4+
import IncreaseComponents from '../components/increaseComponent';
5+
6+
const mapDispatchToProps = actions;
7+
8+
const mapStateToProps = state => ({
9+
increase: state.increase
10+
});
11+
12+
export default connect(mapStateToProps, mapDispatchToProps)(IncreaseComponents);

src/routes/increase/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// import { injectReducer } from 'REDUCER_TOOL';
2+
3+
// export default store => ({
4+
// path: 'counter',
5+
// getComponent(nextState, cb) {
6+
// require.ensure([], (require) => {
7+
// const Counter = require('./containers/CounterContainer').default;
8+
// const reducer = require('./reducers/Counter').default;
9+
// injectReducer(store, { key: 'counter', reducer });
10+
// cb(null, Counter);
11+
// }, 'counter');
12+
// }
13+
// });
14+
import container from './containers/increaseContainer';
15+
import reducer from './reducers/increase';
16+
import component from './components/increaseComponent';
17+
18+
export default component;
19+
export { reducer, container, component };

src/routes/Counter/reducers/Counter.js renamed to src/routes/increase/reducers/increase.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createReducer } from 'REDUCER_TOOL';
1+
import { createReducer } from 'UTILS/reducerTool';
22

33
const COUNTER_INCREMENT = 'COUNTER_INCREMENT';
44
/**
@@ -16,7 +16,7 @@ const incrementBase = (value = 1) => ({
1616
*/
1717
const doubleAsync = () => (dispatch, getState) => {
1818
setTimeout(() => {
19-
dispatch(incrementBase(getState().counter));
19+
dispatch(incrementBase(getState().increase));
2020
}, 1200);
2121
};
2222
const increment = () => incrementBase(1);

src/routes/index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import CoreLayout from 'LAYOUTS/CoreLayout/CoreLayout';
2-
import Home from './Home';
3-
import CounterRoute from './Counter';
2+
import { injectReducerFactory } from 'UTILS/reducerTool';
3+
import Home from './home';
44

5+
const createChildRoute = (path, store) => ({
6+
path,
7+
getComponent(nextState, cb) {
8+
const injectReducer = injectReducerFactory(store);
9+
// System.import(`./${path}/index`).then((routeModule) => {
10+
// const { container, reducer } = routeModule;
11+
// injectReducer(`${path}`, reducer);
12+
// cb(null, container);
13+
// });
14+
require.ensure([], (require) => {
15+
const routeModule = require('./increase');
16+
const { container, reducer } = routeModule;
17+
injectReducer('increase', reducer);
18+
cb(null, container);
19+
}, 'increase');
20+
}
21+
});
22+
// const paths = ['increase'];
523
export const createRoutes = store => ({
624
path: '/',
725
component: CoreLayout,
826
indexRoute: Home,
9-
childRoutes: [
10-
CounterRoute(store)
11-
]
27+
childRoutes: [createChildRoute('increase', store)]
1228
});
1329

1430
export default createRoutes;

src/store/createStore.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { applyMiddleware, compose, createStore } from 'redux';
22
import thunk from 'redux-thunk';
33
import createLogger from 'redux-logger';
4+
import makeRootReducer from 'UTILS/reducerTool';
45
import { browserHistory } from 'react-router';
56
import { updateLocation } from 'REDUCERS/location';
67

7-
import makeRootReducer from './reducerTool';
8-
98
const logger = createLogger();
109
export default (initialState = {}) => {
1110
const middleware = [thunk, logger];
@@ -27,12 +26,14 @@ export default (initialState = {}) => {
2726
);
2827
store.asyncReducers = {};
2928
// store.unsubscribeHistory = browserHistory.listen(updateLocation(store));
30-
// if (module.hot) {
31-
// module.hot.accept('./reducerTool', () => {
32-
// const reducers = require('./reducerTool').default;
33-
// store.replaceReducer(reducers(store.asyncReducers));
34-
// });
35-
// }
29+
if (module.hot) {
30+
module.hot.accept('../utils/reducerTool', () => {
31+
System.import('../utils/reducerTool').then((module) => {
32+
const makeReducer = module.default;
33+
store.replaceReducer(makeReducer(store.asyncReducers));
34+
});
35+
});
36+
}
3637

3738
return store;
3839
};

src/store/reducerTool.js renamed to src/utils/reducerTool.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const makeRootReducer = asyncReducers => combineReducers({
66
...asyncReducers
77
});
88

9-
export const injectReducer = (store, { key, reducer }) => {
10-
if (Object.hasOwnProperty.call(store.asyncReducers, key)) return;
9+
export const injectReducerFactory = store => (key, reducer) => {
10+
if (Reflect.has(store.asyncReducers, key)) return;
1111
store.asyncReducers[key] = reducer;
1212
store.replaceReducer(makeRootReducer(store.asyncReducers));
1313
};

src/utils/request.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'whatwg-fetch';
2+
3+
const globalCallbacks = {};
4+
let globalConifg = {};
5+
export const config = (configs) => {
6+
const callbacksKeys = ['onStart', 'onComplete', 'onSuccess', 'onError'];
7+
callbacksKeys.forEach((key) => {
8+
globalCallbacks[key] = typeof configs[key] === 'function' ? configs[key] : () => {};
9+
delete configs[key];
10+
});
11+
globalConifg = configs;
12+
};
13+
14+
export default (url, options) => {
15+
const { onStart, onComplete, onSuccess, onError } = globalCallbacks;
16+
const promise = new Promise((resolve, reject) => {
17+
onStart();
18+
fetch(url, Object.assign({}, globalConifg, options))
19+
.then((response) => {
20+
if (response.ok) {
21+
response.json().then((result) => {
22+
onSuccess(result);
23+
onComplete(null, result);
24+
resolve(result);
25+
});
26+
} else {
27+
const error = new Error(response.statusText);
28+
error.response = response;
29+
onError(error);
30+
onComplete(error);
31+
reject(error);
32+
}
33+
}, (error) => {
34+
onError(error);
35+
onComplete(error);
36+
reject(error);
37+
});
38+
});
39+
promise.success = (fn) => {
40+
promise.then((result) => {
41+
if (typeof fn === 'function') fn(result);
42+
});
43+
return promise;
44+
};
45+
promise.error = (fn) => {
46+
promise.then(null, (error) => {
47+
if (typeof fn === 'function') fn(error);
48+
});
49+
return promise;
50+
};
51+
promise.complete = (fn) => {
52+
fn = (typeof fn === 'function') ? fn : () => {};
53+
promise.then((result) => {
54+
fn(null, result);
55+
}, (error) => {
56+
fn(error);
57+
});
58+
return promise;
59+
};
60+
return promise;
61+
};

0 commit comments

Comments
 (0)