From 78e5a7d891f4daea258e3d6baf9dce3e23f2bf0e Mon Sep 17 00:00:00 2001 From: Pavlo Aksonov Date: Tue, 9 Feb 2016 16:58:01 +0100 Subject: [PATCH] add more Redux support, demo, issues #56, #152, #158, #167, #183, #184 --- Actions.js | 49 +++++++++++++++++--- ExRouter.js | 3 -- Example/Example.js | 107 ++++++++++++++++++++++++++++--------------- Example/package.json | 6 ++- README.md | 1 + ReactRouter.js | 3 +- Router.js | 14 +++--- debug.js | 2 +- package.json | 2 +- 9 files changed, 128 insertions(+), 59 deletions(-) diff --git a/Actions.js b/Actions.js index 6f32fb6df..4c5bd4e8a 100644 --- a/Actions.js +++ b/Actions.js @@ -2,6 +2,13 @@ import Route from './Route'; import Router from './Router'; import debug from './debug'; +const BEFORE_ROUTE = 'BEFORE_ROUTER_ROUTE'; +const AFTER_ROUTE = 'AFTER_ROUTER_ROUTE'; +const BEFORE_POP = 'BEFORE_ROUTER_POP'; +const AFTER_POP = 'AFTER_ROUTER_POP'; +const BEFORE_DISMISS = 'BEFORE_ROUTER_DISMISS'; +const AFTER_DISMISS = 'AFTER_ROUTER_DISMISS'; + function isNumeric(n){ return !isNaN(parseFloat(n)) && isFinite(n); } @@ -58,8 +65,12 @@ class Actions { router = route.parent; debug("Switching to router="+router.name); } + debug("ROUTER DELEGATE PROPS:"+ router.delegate.props.dispatch) + const currentRoute = router.routes[name]; + if (router.delegate.props && router.delegate.props.dispatch){ + router.delegate.props.dispatch({...props, type: BEFORE_ROUTE, route:currentRoute, name}) + } if (router.route(name, props)){ - // deep into child router while (router.currentRoute.childRouter){ router = router.currentRoute.childRouter; @@ -67,20 +78,32 @@ class Actions { } this.currentRouter = router; + if (router.delegate.props && router.delegate.props.dispatch){ + router.delegate.props.dispatch({...props, type: AFTER_ROUTE, route:currentRoute, name}) + } return true; } return false; } - dismiss(){ + dismiss(props: { [key: string]: any} = {}){ + props = filterParam(props); let router: Router = this.currentRouter; // go to root router while (router.parentRoute){ router = router.parentRoute.parent; debug("Switching to parent router="+router.name); } - return router.dismiss(); + if (router.delegate.props && router.delegate.props.dispatch){ + router.delegate.props.dispatch({...props, type: BEFORE_DISMISS, route:router.currentRoute, name:router.currentRoute.name}) + } + const res = router.dismiss(); + if (router.delegate.props && router.delegate.props.dispatch){ + router.delegate.props.dispatch({...props, type: AFTER_DISMISS, route:router.currentRoute, name:router.currentRoute.name}) + } + return res; } - pop(num: number = 1){ + pop(num: number = 1, props: { [key: string]: any} = {}){ + props = filterParam(props); if (!isNumeric(num)){ num = 1; } @@ -106,8 +129,14 @@ class Actions { break; } } - if (router.pop()){ + if (router.delegate.props && router.delegate.props.dispatch){ + router.delegate.props.dispatch({...props, type: BEFORE_POP, route:router.currentRoute, name:router.currentRoute.name}) + } + if (router.pop(1, props)){ this.currentRouter = router; + if (router.delegate.props && router.delegate.props.dispatch){ + router.delegate.props.dispatch({...props, type: AFTER_POP, route:router.currentRoute, name:router.currentRoute.name}) + } return true; } else { return false; @@ -116,5 +145,11 @@ class Actions { } } } - -export default new Actions(); +const actions = new Actions(); +actions.BEFORE_ROUTE = BEFORE_ROUTE; +actions.AFTER_ROUTE = AFTER_ROUTE; +actions.BEFORE_POP = BEFORE_POP; +actions.AFTER_POP = AFTER_POP; +actions.BEFORE_DISMISS = BEFORE_DISMISS; +actions.AFTER_DISMISS = AFTER_DISMISS; +export default actions; diff --git a/ExRouter.js b/ExRouter.js index 6ec8f978e..8af135c57 100644 --- a/ExRouter.js +++ b/ExRouter.js @@ -284,9 +284,6 @@ export default class ExRouter extends React.Component { const Footer = this.props.footer; const footer = Footer ?