@@ -55,14 +55,23 @@ function defaultAbortHandler(abortReason, location) {
55
55
}
56
56
}
57
57
58
- function findMatch ( pathname , routes , defaultRoute , notFoundRoute ) {
59
- var match , route , params ;
58
+ function createMatch ( route , params , pathname , query ) {
59
+ return {
60
+ routes : [ route ] ,
61
+ params : params ,
62
+ pathname : pathname ,
63
+ query : query
64
+ } ;
65
+ }
66
+
67
+ function findMatch ( routes , defaultRoute , notFoundRoute , pathname , query ) {
68
+ var route , match , params ;
60
69
61
70
for ( var i = 0 , len = routes . length ; i < len ; ++ i ) {
62
71
route = routes [ i ] ;
63
72
64
73
// Check the subtree first to find the most deeply-nested match.
65
- match = findMatch ( pathname , route . childRoutes , route . defaultRoute , route . notFoundRoute ) ;
74
+ match = findMatch ( route . routes , route . defaultRoute , route . notFoundRoute , pathname , query ) ;
66
75
67
76
if ( match != null ) {
68
77
match . routes . unshift ( route ) ;
@@ -73,22 +82,18 @@ function findMatch(pathname, routes, defaultRoute, notFoundRoute) {
73
82
params = Path . extractParams ( route . path , pathname ) ;
74
83
75
84
if ( params )
76
- return createMatch ( route , params ) ;
85
+ return createMatch ( route , params , pathname , query ) ;
77
86
}
78
87
79
88
// No routes matched, so try the default route if there is one.
80
89
if ( defaultRoute && ( params = Path . extractParams ( defaultRoute . path , pathname ) ) )
81
- return createMatch ( defaultRoute , params ) ;
90
+ return createMatch ( defaultRoute , params , pathname , query ) ;
82
91
83
92
// Last attempt: does the "not found" route match?
84
93
if ( notFoundRoute && ( params = Path . extractParams ( notFoundRoute . path , pathname ) ) )
85
- return createMatch ( notFoundRoute , params ) ;
86
-
87
- return match ;
88
- }
94
+ return createMatch ( notFoundRoute , params , pathname , query ) ;
89
95
90
- function createMatch ( route , params ) {
91
- return { routes : [ route ] , params : params } ;
96
+ return null ;
92
97
}
93
98
94
99
function hasProperties ( object , properties ) {
@@ -144,8 +149,6 @@ function createRouter(options) {
144
149
if ( isReactChildren ( options ) )
145
150
options = { routes : options } ;
146
151
147
- var routes = [ ] ;
148
- var namedRoutes = { } ;
149
152
var mountedComponents = [ ] ;
150
153
var location = options . location || DEFAULT_LOCATION ;
151
154
var scrollBehavior = options . scrollBehavior || DEFAULT_SCROLL_BEHAVIOR ;
@@ -157,13 +160,6 @@ function createRouter(options) {
157
160
var dispatchHandler = null ;
158
161
var changeListener = null ;
159
162
160
- function cancelPendingTransition ( ) {
161
- if ( pendingTransition ) {
162
- pendingTransition . abort ( new Cancellation ) ;
163
- pendingTransition = null ;
164
- }
165
- }
166
-
167
163
if ( typeof location === 'string' ) {
168
164
warning (
169
165
! canUseDOM || process . env . NODE_ENV === 'test' ,
@@ -183,41 +179,57 @@ function createRouter(options) {
183
179
if ( location === HistoryLocation && ! supportsHistory ( ) )
184
180
location = RefreshLocation ;
185
181
186
- var router = React . createClass ( {
182
+ var Router = React . createClass ( {
187
183
188
184
displayName : 'Router' ,
189
185
190
- mixins : [ NavigationContext , StateContext , Scrolling ] ,
191
-
192
186
statics : {
193
187
194
- defaultRoute : null ,
195
- notFoundRoute : null ,
196
188
isRunning : false ,
197
189
190
+ cancelPendingTransition : function ( ) {
191
+ if ( pendingTransition ) {
192
+ pendingTransition . abort ( new Cancellation ) ;
193
+ pendingTransition = null ;
194
+ }
195
+ } ,
196
+
197
+ clearAllRoutes : function ( ) {
198
+ this . cancelPendingTransition ( ) ;
199
+ this . defaultRoute = null ;
200
+ this . notFoundRoute = null ;
201
+ this . namedRoutes = { } ;
202
+ this . routes = [ ] ;
203
+ } ,
204
+
198
205
/**
199
206
* Adds routes to this router from the given children object (see ReactChildren).
200
207
*/
201
- addRoutes : function ( newRoutes ) {
202
- if ( isReactChildren ( newRoutes ) )
203
- newRoutes = createRoutesFromReactChildren ( newRoutes , this , namedRoutes ) ;
208
+ addRoutes : function ( routes ) {
209
+ if ( isReactChildren ( routes ) )
210
+ routes = createRoutesFromReactChildren ( routes , this , this . namedRoutes ) ;
204
211
205
- routes . push . apply ( routes , newRoutes ) ;
212
+ this . routes . push . apply ( this . routes , routes ) ;
206
213
} ,
207
214
208
215
/**
209
216
* Replaces routes of this router from the given children object (see ReactChildren).
210
217
*/
211
- replaceRoutes : function ( newRoutes ) {
212
- cancelPendingTransition ( ) ;
213
-
214
- routes = [ ] ;
215
- namedRoutes = { } ;
216
-
217
- this . addRoutes ( newRoutes ) ;
218
+ replaceRoutes : function ( routes ) {
219
+ this . clearAllRoutes ( ) ;
220
+ this . addRoutes ( routes ) ;
218
221
this . refresh ( ) ;
219
222
} ,
220
223
224
+ /**
225
+ * Performs a match of the given path against this router and returns an object
226
+ * with the { routes, params, pathname, query } that match. Returns null if no
227
+ * match can be made.
228
+ */
229
+ match : function ( path ) {
230
+ return findMatch ( this . routes , this . defaultRoute , this . notFoundRoute , Path . withoutQuery ( path ) , Path . extractQuery ( path ) ) ;
231
+ } ,
232
+
221
233
/**
222
234
* Returns an absolute URL path created from the given route
223
235
* name, URL parameters, and query.
@@ -227,7 +239,7 @@ function createRouter(options) {
227
239
if ( Path . isAbsolute ( to ) ) {
228
240
path = Path . normalize ( to ) ;
229
241
} else {
230
- var route = namedRoutes [ to ] ;
242
+ var route = this . namedRoutes [ to ] ;
231
243
232
244
invariant (
233
245
route ,
@@ -310,14 +322,6 @@ function createRouter(options) {
310
322
return false ;
311
323
} ,
312
324
313
- /**
314
- * Performs a match of the given pathname against this router and returns an object
315
- * with the { routes, params } that match. Returns null if no match can be made.
316
- */
317
- match : function ( pathname ) {
318
- return findMatch ( pathname , routes , this . defaultRoute , this . notFoundRoute ) || null ;
319
- } ,
320
-
321
325
/**
322
326
* Performs a transition to the given path and calls callback(error, abortReason)
323
327
* when the transition is finished. If both arguments are null the router's state
@@ -335,7 +339,7 @@ function createRouter(options) {
335
339
* hooks wait, the transition is fully synchronous.
336
340
*/
337
341
dispatch : function ( path , action ) {
338
- cancelPendingTransition ( ) ;
342
+ this . cancelPendingTransition ( ) ;
339
343
340
344
var prevPath = state . path ;
341
345
var isRefreshing = action == null ;
@@ -348,8 +352,7 @@ function createRouter(options) {
348
352
if ( prevPath && action !== LocationActions . REPLACE )
349
353
this . recordScrollPosition ( prevPath ) ;
350
354
351
- var pathname = Path . withoutQuery ( path ) ;
352
- var match = this . match ( pathname ) ;
355
+ var match = this . match ( path ) ;
353
356
354
357
warning (
355
358
match != null ,
@@ -366,7 +369,7 @@ function createRouter(options) {
366
369
367
370
var nextRoutes = match . routes || [ ] ;
368
371
var nextParams = match . params || { } ;
369
- var nextQuery = Path . extractQuery ( path ) || { } ;
372
+ var nextQuery = match . query || { } ;
370
373
371
374
var fromRoutes , toRoutes ;
372
375
if ( prevRoutes . length ) {
@@ -389,22 +392,22 @@ function createRouter(options) {
389
392
390
393
transition . from ( fromRoutes , fromComponents , function ( error ) {
391
394
if ( error || transition . abortReason )
392
- return dispatchHandler . call ( router , error , transition ) ;
395
+ return dispatchHandler . call ( Router , error , transition ) ;
393
396
394
397
transition . to ( toRoutes , nextParams , nextQuery , function ( error ) {
395
398
if ( error || transition . abortReason )
396
- return dispatchHandler . call ( router , error , transition ) ;
399
+ return dispatchHandler . call ( Router , error , transition ) ;
397
400
398
401
nextState = {
399
402
path : path ,
400
403
action : action ,
401
- pathname : pathname ,
404
+ pathname : match . pathname ,
402
405
routes : nextRoutes ,
403
406
params : nextParams ,
404
407
query : nextQuery
405
408
} ;
406
409
407
- dispatchHandler . call ( router , null , transition ) ;
410
+ dispatchHandler . call ( Router , null , transition ) ;
408
411
} ) ;
409
412
} ) ;
410
413
} ,
@@ -424,26 +427,26 @@ function createRouter(options) {
424
427
425
428
dispatchHandler = function ( error , transition ) {
426
429
if ( error )
427
- onError . call ( router , error ) ;
430
+ onError . call ( Router , error ) ;
428
431
429
432
if ( pendingTransition !== transition )
430
433
return ;
431
434
432
435
pendingTransition = null ;
433
436
434
437
if ( transition . abortReason ) {
435
- onAbort . call ( router , transition . abortReason , location ) ;
438
+ onAbort . call ( Router , transition . abortReason , location ) ;
436
439
} else {
437
- callback . call ( router , router , nextState ) ;
440
+ callback . call ( Router , Router , nextState ) ;
438
441
}
439
442
} ;
440
443
441
444
if ( typeof location === 'string' ) {
442
- router . dispatch ( location , null ) ;
445
+ Router . dispatch ( location , null ) ;
443
446
} else {
444
447
// Listen for changes to the location.
445
448
changeListener = function ( change ) {
446
- router . dispatch ( change . path , change . type ) ;
449
+ Router . dispatch ( change . path , change . type ) ;
447
450
} ;
448
451
449
452
if ( location . addChangeListener )
@@ -457,7 +460,7 @@ function createRouter(options) {
457
460
} ,
458
461
459
462
stop : function ( ) {
460
- cancelPendingTransition ( ) ;
463
+ this . cancelPendingTransition ( ) ;
461
464
462
465
if ( location . removeChangeListener && changeListener ) {
463
466
location . removeChangeListener ( changeListener ) ;
@@ -468,11 +471,13 @@ function createRouter(options) {
468
471
} ,
469
472
470
473
refresh : function ( ) {
471
- router . dispatch ( location . getCurrentPath ( ) , null ) ;
474
+ Router . dispatch ( location . getCurrentPath ( ) , null ) ;
472
475
}
473
476
474
477
} ,
475
478
479
+ mixins : [ NavigationContext , StateContext , Scrolling ] ,
480
+
476
481
propTypes : {
477
482
children : PropTypes . falsy
478
483
} ,
@@ -503,7 +508,7 @@ function createRouter(options) {
503
508
} ,
504
509
505
510
componentWillUnmount : function ( ) {
506
- router . stop ( ) ;
511
+ Router . stop ( ) ;
507
512
} ,
508
513
509
514
render : function ( ) {
@@ -527,10 +532,12 @@ function createRouter(options) {
527
532
528
533
} ) ;
529
534
535
+ Router . clearAllRoutes ( ) ;
536
+
530
537
if ( options . routes )
531
- router . addRoutes ( options . routes ) ;
538
+ Router . addRoutes ( options . routes ) ;
532
539
533
- return router ;
540
+ return Router ;
534
541
}
535
542
536
543
module . exports = createRouter ;
0 commit comments