Skip to content

Commit 10359a5

Browse files
committed
Add Route object
1 parent 745643b commit 10359a5

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

modules/createRoutesFromReactChildren.js renamed to modules/Routing.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@ var NotFoundRoute = require('./components/NotFoundRoute');
66
var Redirect = require('./components/Redirect');
77
var Path = require('./utils/Path');
88

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+
};
2113
}
2214

2315
function createRoute(element, parentRoute, namedRoutes) {
@@ -27,16 +19,18 @@ function createRoute(element, parentRoute, namedRoutes) {
2719
if (type.validateProps)
2820
type.validateProps(props);
2921

30-
var route = {
22+
var options = {
3123
name: props.name,
3224
ignoreScrollBehavior: !!props.ignoreScrollBehavior
3325
};
3426

3527
if (type === Redirect.type) {
36-
route.handler = createRedirectHandler(props.to, props.params, props.query);
28+
options.willTransitionTo = createTransitionToHook(props.to, props.params, props.query);
3729
props.path = props.path || props.from || '*';
3830
} 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;
4034
}
4135

4236
var parentPath = (parentRoute && parentRoute.path) || '/';
@@ -48,27 +42,29 @@ function createRoute(element, parentRoute, namedRoutes) {
4842
if (!Path.isAbsolute(path))
4943
path = Path.join(parentPath, path);
5044

51-
route.path = Path.normalize(path);
45+
options.path = Path.normalize(path);
5246
} else {
53-
route.path = parentPath;
47+
options.path = parentPath;
5448

5549
if (type === NotFoundRoute.type)
56-
route.path += '*';
50+
options.path += '*';
5751
}
5852

59-
route.paramNames = Path.extractParamNames(route.path);
53+
options.paramNames = Path.extractParamNames(options.path);
6054

6155
// Make sure the route's path has all params its parent needs.
6256
if (parentRoute && Array.isArray(parentRoute.paramNames)) {
6357
parentRoute.paramNames.forEach(function (paramName) {
6458
invariant(
65-
route.paramNames.indexOf(paramName) !== -1,
59+
options.paramNames.indexOf(paramName) !== -1,
6660
'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
6862
);
6963
});
7064
}
7165

66+
var route = new Route(options);
67+
7268
// Make sure the route can be looked up by <Link>s.
7369
if (props.name) {
7470
invariant(
@@ -144,4 +140,19 @@ function createRoutesFromReactChildren(children, parentRoute, namedRoutes) {
144140
return routes;
145141
}
146142

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+
};

modules/Transition.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ assign(Transition.prototype, {
3232
return function (error) {
3333
if (error || self.abortReason) {
3434
callback(error);
35-
} else if (route.handler.willTransitionFrom) {
35+
} else if (route.willTransitionFrom) {
3636
try {
37-
route.handler.willTransitionFrom(self, components[index], callback);
37+
route.willTransitionFrom(self, components[index], callback);
3838

3939
// If there is no callback in the argument list, call it automatically.
40-
if (route.handler.willTransitionFrom.length < 3)
40+
if (route.willTransitionFrom.length < 3)
4141
callback();
4242
} catch (e) {
4343
callback(e);
@@ -58,12 +58,12 @@ assign(Transition.prototype, {
5858
return function (error) {
5959
if (error || self.abortReason) {
6060
callback(error);
61-
} else if (route.handler.willTransitionTo) {
61+
} else if (route.willTransitionTo) {
6262
try {
63-
route.handler.willTransitionTo(self, params, query, callback);
63+
route.willTransitionTo(self, params, query, callback);
6464

6565
// If there is no callback in the argument list, call it automatically.
66-
if (route.handler.willTransitionTo.length < 4)
66+
if (route.willTransitionTo.length < 4)
6767
callback();
6868
} catch (e) {
6969
callback(e);
File renamed without changes.

modules/createRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var RefreshLocation = require('./locations/RefreshLocation');
1111
var NavigationContext = require('./NavigationContext');
1212
var StateContext = require('./StateContext');
1313
var Scrolling = require('./Scrolling');
14-
var createRoutesFromReactChildren = require('./createRoutesFromReactChildren');
14+
var createRoutesFromReactChildren = require('./Routing').createRoutesFromReactChildren;
1515
var isReactChildren = require('./isReactChildren');
1616
var Transition = require('./Transition');
1717
var PropTypes = require('./PropTypes');

0 commit comments

Comments
 (0)