Skip to content

Commit

Permalink
parse params with path-to-regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Aug 30, 2016
1 parent baba191 commit 3314c7c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/models/item/index.js
Expand Up @@ -66,11 +66,14 @@ export default {
});
}

return history.listen(({ pathname }, { params }) => {
return history.listen(({ pathname }) => {
for (const type of ITEM_TYPES) {
if (pathToRegexp(`/${type}/:page?`).test(pathname)) {
const match = pathToRegexp(`/${type}/:page?`).exec(pathname);
if (match) {
const page = match[1];

// fetch
fetchList(type, params.page);
fetchList(type, page);

// watch
if (activeType !== type) {
Expand Down
12 changes: 7 additions & 5 deletions src/models/user/index.js
Expand Up @@ -8,18 +8,20 @@ export default {
},
subscriptions: {
setup({ dispatch, history }) {
history.listen(({ pathname }, { params }) => {
if (pathToRegexp('/user/:userId').test(pathname)) {
history.listen(({ pathname }) => {
const match = pathToRegexp('/user/:userId').exec(pathname);
if (match) {
const userId = match[1];
dispatch({
type: 'user/fetchUser',
payload: params.userId,
type: 'fetchUser',
payload: userId,
});
}
});
},
},
effects: {
*fetchUser({ payload: id }) {
*fetchUser({ payload: id }, { call, put }) {
yield put({ type: 'app/showLoading' });
const user = yield call(fetchUser, id);
yield put({
Expand Down

0 comments on commit 3314c7c

Please sign in to comment.