Skip to content

Commit 7278dc5

Browse files
committed
Return { routes, params } from match
1 parent 841dfd1 commit 7278dc5

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

modules/locations/HashLocation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ var _changeListeners = [];
2222

2323
function notifyChange(type) {
2424
var change = {
25-
type: type,
26-
path: getHashPath()
25+
path: getHashPath(),
26+
type: type
2727
};
2828

2929
_changeListeners.forEach(function (listener) {

modules/locations/HistoryLocation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var _changeListeners = [];
55

66
function notifyChange(type) {
77
var change = {
8-
type: type,
9-
path: getWindowPath()
8+
path: getWindowPath(),
9+
type: type
1010
};
1111

1212
_changeListeners.forEach(function (listener) {

modules/locations/TestLocation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var _listener;
44

55
function notifyChange(type) {
66
if (_listener)
7-
_listener({ type: type, path: TestLocation.getCurrentPath() });
7+
_listener({ path: TestLocation.getCurrentPath(), type: type });
88
}
99

1010
/**

modules/utils/createRouter.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function findMatch(pathname, routes, defaultRoute, notFoundRoute) {
8282
}
8383

8484
function createMatch(route, params) {
85-
return { path: null, routes: [ route ], params: params, query: null };
85+
return { routes: [ route ], params: params };
8686
}
8787

8888
function hasMatch(routes, route, prevParams, nextParams) {
@@ -238,16 +238,7 @@ function createRouter(options) {
238238
* the { path, routes, params, query } that match. Returns null if no match can be made.
239239
*/
240240
match: function (path) {
241-
var pathname = Path.withoutQuery(path);
242-
var match = findMatch(pathname, routes, this.defaultRoute, this.notFoundRoute);
243-
244-
if (match == null)
245-
return null;
246-
247-
match.path = path;
248-
match.query = Path.extractQuery(path) || {};
249-
250-
return match;
241+
return findMatch(Path.withoutQuery(path), routes, this.defaultRoute, this.notFoundRoute) || null;
251242
},
252243

253244
/**
@@ -266,27 +257,27 @@ function createRouter(options) {
266257
* transition. To resolve asynchronously, they may use transition.wait(promise). If no
267258
* hooks wait, the transition is fully synchronous.
268259
*/
269-
dispatch: function (action, path, callback) {
260+
dispatch: function (path, action, callback) {
270261
if (state.path === path)
271262
return; // Nothing to do!
272263

273-
var nextState = this.match(path);
264+
var match = this.match(path);
274265

275266
warning(
276-
nextState != null,
267+
match != null,
277268
'No route matches path "%s". Make sure you have <Route path="%s"> somewhere in your routes',
278269
path, path
279270
);
280271

281-
if (nextState == null)
282-
nextState = {};
272+
if (match == null)
273+
match = {};
283274

284275
var prevRoutes = state.routes || [];
285276
var prevParams = state.params || {};
286277

287-
var nextRoutes = nextState.routes || [];
288-
var nextParams = nextState.params || {};
289-
var nextQuery = nextState.query || {};
278+
var nextRoutes = match.routes || [];
279+
var nextParams = match.params || {};
280+
var nextQuery = Path.extractQuery(path) || {};
290281

291282
var fromRoutes, toRoutes;
292283
if (prevRoutes.length) {
@@ -312,8 +303,11 @@ function createRouter(options) {
312303
if (error || transition.isAborted)
313304
return callback.call(router, error, transition);
314305

315-
state = nextState;
306+
state.path = path;
316307
state.action = action;
308+
state.routes = nextRoutes;
309+
state.params = nextParams;
310+
state.query = nextQuery;
317311

318312
callback.call(router, null, transition);
319313
});
@@ -346,7 +340,7 @@ function createRouter(options) {
346340
);
347341

348342
// Dispatch the location.
349-
router.dispatch(null, location, dispatchHandler);
343+
router.dispatch(location, null, dispatchHandler);
350344
} else {
351345
invariant(
352346
canUseDOM,
@@ -356,14 +350,14 @@ function createRouter(options) {
356350

357351
// Listen for changes to the location.
358352
function changeListener(change) {
359-
router.dispatch(change.type, change.path, dispatchHandler);
353+
router.dispatch(change.path, change.type, dispatchHandler);
360354
}
361355

362356
if (location.addChangeListener)
363357
location.addChangeListener(changeListener);
364358

365359
// Bootstrap using the current path.
366-
router.dispatch(null, location.getCurrentPath(), dispatchHandler);
360+
router.dispatch(location.getCurrentPath(), null, dispatchHandler);
367361
}
368362
}
369363

0 commit comments

Comments
 (0)