@@ -6,18 +6,10 @@ var NotFoundRoute = require('./components/NotFoundRoute');
6
6
var Redirect = require ( './components/Redirect' ) ;
7
7
var Path = require ( './utils/Path' ) ;
8
8
9
- function createRedirectHandler ( to , _params , _query ) {
10
- return React . createClass ( {
11
- statics : {
12
- willTransitionTo : function ( transition , params , query ) {
13
- transition . redirect ( to , _params || params , _query || query ) ;
14
- }
15
- } ,
16
-
17
- render : function ( ) {
18
- return null ;
19
- }
20
- } ) ;
9
+ function createTransitionToHook ( to , _params , _query ) {
10
+ return function ( transition , params , query ) {
11
+ transition . redirect ( to , _params || params , _query || query ) ;
12
+ } ;
21
13
}
22
14
23
15
function createRoute ( element , parentRoute , namedRoutes ) {
@@ -27,16 +19,18 @@ function createRoute(element, parentRoute, namedRoutes) {
27
19
if ( type . validateProps )
28
20
type . validateProps ( props ) ;
29
21
30
- var route = {
22
+ var options = {
31
23
name : props . name ,
32
24
ignoreScrollBehavior : ! ! props . ignoreScrollBehavior
33
25
} ;
34
26
35
27
if ( type === Redirect . type ) {
36
- route . handler = createRedirectHandler ( props . to , props . params , props . query ) ;
28
+ options . willTransitionTo = createTransitionToHook ( props . to , props . params , props . query ) ;
37
29
props . path = props . path || props . from || '*' ;
38
30
} else {
39
- route . handler = props . handler ;
31
+ options . handler = props . handler ;
32
+ options . willTransitionTo = props . handler && props . handler . willTransitionTo ;
33
+ options . willTransitionFrom = props . handler && props . handler . willTransitionFrom ;
40
34
}
41
35
42
36
var parentPath = ( parentRoute && parentRoute . path ) || '/' ;
@@ -48,27 +42,29 @@ function createRoute(element, parentRoute, namedRoutes) {
48
42
if ( ! Path . isAbsolute ( path ) )
49
43
path = Path . join ( parentPath , path ) ;
50
44
51
- route . path = Path . normalize ( path ) ;
45
+ options . path = Path . normalize ( path ) ;
52
46
} else {
53
- route . path = parentPath ;
47
+ options . path = parentPath ;
54
48
55
49
if ( type === NotFoundRoute . type )
56
- route . path += '*' ;
50
+ options . path += '*' ;
57
51
}
58
52
59
- route . paramNames = Path . extractParamNames ( route . path ) ;
53
+ options . paramNames = Path . extractParamNames ( options . path ) ;
60
54
61
55
// Make sure the route's path has all params its parent needs.
62
56
if ( parentRoute && Array . isArray ( parentRoute . paramNames ) ) {
63
57
parentRoute . paramNames . forEach ( function ( paramName ) {
64
58
invariant (
65
- route . paramNames . indexOf ( paramName ) !== - 1 ,
59
+ options . paramNames . indexOf ( paramName ) !== - 1 ,
66
60
'The nested route path "%s" is missing the "%s" parameter of its parent path "%s"' ,
67
- route . path , paramName , parentRoute . path
61
+ options . path , paramName , parentRoute . path
68
62
) ;
69
63
} ) ;
70
64
}
71
65
66
+ var route = new Route ( options ) ;
67
+
72
68
// Make sure the route can be looked up by <Link>s.
73
69
if ( props . name ) {
74
70
invariant (
@@ -144,4 +140,19 @@ function createRoutesFromReactChildren(children, parentRoute, namedRoutes) {
144
140
return routes ;
145
141
}
146
142
147
- module . exports = createRoutesFromReactChildren ;
143
+ function Route ( options ) {
144
+ options = options || { } ;
145
+
146
+ this . name = options . name ;
147
+ this . path = options . path || '/' ;
148
+ this . paramNames = options . paramNames || Path . extractParamNames ( this . path ) ;
149
+ this . ignoreScrollBehavior = ! ! options . ignoreScrollBehavior ;
150
+ this . willTransitionTo = options . willTransitionTo ;
151
+ this . willTransitionFrom = options . willTransitionFrom ;
152
+ this . handler = options . handler ;
153
+ }
154
+
155
+ module . exports = {
156
+ createRoutesFromReactChildren : createRoutesFromReactChildren ,
157
+ Route : Route
158
+ } ;
0 commit comments