Skip to content

Commit 7672f7b

Browse files
committed
Add pathname/query to match objects
1 parent 5396b1c commit 7672f7b

File tree

2 files changed

+71
-64
lines changed

2 files changed

+71
-64
lines changed

modules/createRouter.js

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@ function defaultAbortHandler(abortReason, location) {
5555
}
5656
}
5757

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;
6069

6170
for (var i = 0, len = routes.length; i < len; ++i) {
6271
route = routes[i];
6372

6473
// 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);
6675

6776
if (match != null) {
6877
match.routes.unshift(route);
@@ -73,22 +82,18 @@ function findMatch(pathname, routes, defaultRoute, notFoundRoute) {
7382
params = Path.extractParams(route.path, pathname);
7483

7584
if (params)
76-
return createMatch(route, params);
85+
return createMatch(route, params, pathname, query);
7786
}
7887

7988
// No routes matched, so try the default route if there is one.
8089
if (defaultRoute && (params = Path.extractParams(defaultRoute.path, pathname)))
81-
return createMatch(defaultRoute, params);
90+
return createMatch(defaultRoute, params, pathname, query);
8291

8392
// Last attempt: does the "not found" route match?
8493
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);
8995

90-
function createMatch(route, params) {
91-
return { routes: [ route ], params: params };
96+
return null;
9297
}
9398

9499
function hasProperties(object, properties) {
@@ -144,8 +149,6 @@ function createRouter(options) {
144149
if (isReactChildren(options))
145150
options = { routes: options };
146151

147-
var routes = [];
148-
var namedRoutes = {};
149152
var mountedComponents = [];
150153
var location = options.location || DEFAULT_LOCATION;
151154
var scrollBehavior = options.scrollBehavior || DEFAULT_SCROLL_BEHAVIOR;
@@ -157,13 +160,6 @@ function createRouter(options) {
157160
var dispatchHandler = null;
158161
var changeListener = null;
159162

160-
function cancelPendingTransition() {
161-
if (pendingTransition) {
162-
pendingTransition.abort(new Cancellation);
163-
pendingTransition = null;
164-
}
165-
}
166-
167163
if (typeof location === 'string') {
168164
warning(
169165
!canUseDOM || process.env.NODE_ENV === 'test',
@@ -183,41 +179,57 @@ function createRouter(options) {
183179
if (location === HistoryLocation && !supportsHistory())
184180
location = RefreshLocation;
185181

186-
var router = React.createClass({
182+
var Router = React.createClass({
187183

188184
displayName: 'Router',
189185

190-
mixins: [ NavigationContext, StateContext, Scrolling ],
191-
192186
statics: {
193187

194-
defaultRoute: null,
195-
notFoundRoute: null,
196188
isRunning: false,
197189

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+
198205
/**
199206
* Adds routes to this router from the given children object (see ReactChildren).
200207
*/
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);
204211

205-
routes.push.apply(routes, newRoutes);
212+
this.routes.push.apply(this.routes, routes);
206213
},
207214

208215
/**
209216
* Replaces routes of this router from the given children object (see ReactChildren).
210217
*/
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);
218221
this.refresh();
219222
},
220223

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+
221233
/**
222234
* Returns an absolute URL path created from the given route
223235
* name, URL parameters, and query.
@@ -227,7 +239,7 @@ function createRouter(options) {
227239
if (Path.isAbsolute(to)) {
228240
path = Path.normalize(to);
229241
} else {
230-
var route = namedRoutes[to];
242+
var route = this.namedRoutes[to];
231243

232244
invariant(
233245
route,
@@ -310,14 +322,6 @@ function createRouter(options) {
310322
return false;
311323
},
312324

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-
321325
/**
322326
* Performs a transition to the given path and calls callback(error, abortReason)
323327
* when the transition is finished. If both arguments are null the router's state
@@ -335,7 +339,7 @@ function createRouter(options) {
335339
* hooks wait, the transition is fully synchronous.
336340
*/
337341
dispatch: function (path, action) {
338-
cancelPendingTransition();
342+
this.cancelPendingTransition();
339343

340344
var prevPath = state.path;
341345
var isRefreshing = action == null;
@@ -348,8 +352,7 @@ function createRouter(options) {
348352
if (prevPath && action !== LocationActions.REPLACE)
349353
this.recordScrollPosition(prevPath);
350354

351-
var pathname = Path.withoutQuery(path);
352-
var match = this.match(pathname);
355+
var match = this.match(path);
353356

354357
warning(
355358
match != null,
@@ -366,7 +369,7 @@ function createRouter(options) {
366369

367370
var nextRoutes = match.routes || [];
368371
var nextParams = match.params || {};
369-
var nextQuery = Path.extractQuery(path) || {};
372+
var nextQuery = match.query || {};
370373

371374
var fromRoutes, toRoutes;
372375
if (prevRoutes.length) {
@@ -389,22 +392,22 @@ function createRouter(options) {
389392

390393
transition.from(fromRoutes, fromComponents, function (error) {
391394
if (error || transition.abortReason)
392-
return dispatchHandler.call(router, error, transition);
395+
return dispatchHandler.call(Router, error, transition);
393396

394397
transition.to(toRoutes, nextParams, nextQuery, function (error) {
395398
if (error || transition.abortReason)
396-
return dispatchHandler.call(router, error, transition);
399+
return dispatchHandler.call(Router, error, transition);
397400

398401
nextState = {
399402
path: path,
400403
action: action,
401-
pathname: pathname,
404+
pathname: match.pathname,
402405
routes: nextRoutes,
403406
params: nextParams,
404407
query: nextQuery
405408
};
406409

407-
dispatchHandler.call(router, null, transition);
410+
dispatchHandler.call(Router, null, transition);
408411
});
409412
});
410413
},
@@ -424,26 +427,26 @@ function createRouter(options) {
424427

425428
dispatchHandler = function (error, transition) {
426429
if (error)
427-
onError.call(router, error);
430+
onError.call(Router, error);
428431

429432
if (pendingTransition !== transition)
430433
return;
431434

432435
pendingTransition = null;
433436

434437
if (transition.abortReason) {
435-
onAbort.call(router, transition.abortReason, location);
438+
onAbort.call(Router, transition.abortReason, location);
436439
} else {
437-
callback.call(router, router, nextState);
440+
callback.call(Router, Router, nextState);
438441
}
439442
};
440443

441444
if (typeof location === 'string') {
442-
router.dispatch(location, null);
445+
Router.dispatch(location, null);
443446
} else {
444447
// Listen for changes to the location.
445448
changeListener = function (change) {
446-
router.dispatch(change.path, change.type);
449+
Router.dispatch(change.path, change.type);
447450
};
448451

449452
if (location.addChangeListener)
@@ -457,7 +460,7 @@ function createRouter(options) {
457460
},
458461

459462
stop: function () {
460-
cancelPendingTransition();
463+
this.cancelPendingTransition();
461464

462465
if (location.removeChangeListener && changeListener) {
463466
location.removeChangeListener(changeListener);
@@ -468,11 +471,13 @@ function createRouter(options) {
468471
},
469472

470473
refresh: function () {
471-
router.dispatch(location.getCurrentPath(), null);
474+
Router.dispatch(location.getCurrentPath(), null);
472475
}
473476

474477
},
475478

479+
mixins: [ NavigationContext, StateContext, Scrolling ],
480+
476481
propTypes: {
477482
children: PropTypes.falsy
478483
},
@@ -503,7 +508,7 @@ function createRouter(options) {
503508
},
504509

505510
componentWillUnmount: function () {
506-
router.stop();
511+
Router.stop();
507512
},
508513

509514
render: function () {
@@ -527,10 +532,12 @@ function createRouter(options) {
527532

528533
});
529534

535+
Router.clearAllRoutes();
536+
530537
if (options.routes)
531-
router.addRoutes(options.routes);
538+
Router.addRoutes(options.routes);
532539

533-
return router;
540+
return Router;
534541
}
535542

536543
module.exports = createRouter;

modules/createRoutesFromReactChildren.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function createRoute(element, parentRoute, namedRoutes) {
126126
return null;
127127
}
128128

129-
route.childRoutes = createRoutesFromReactChildren(props.children, route, namedRoutes);
129+
route.routes = createRoutesFromReactChildren(props.children, route, namedRoutes);
130130

131131
return route;
132132
}

0 commit comments

Comments
 (0)