Skip to content

Commit 283d3f6

Browse files
committed
[added] RouteStore#registerChildren
1 parent a030648 commit 283d3f6

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

modules/components/Routes.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,10 @@ var Routes = React.createClass({
9595

9696
getInitialState: function () {
9797
return {
98-
routes: this.getRoutes()
98+
routes: RouteStore.registerChildren(this.props.children, this)
9999
};
100100
},
101101

102-
getRoutes: function () {
103-
var routes = [];
104-
105-
React.Children.forEach(this.props.children, function (child) {
106-
if (child = RouteStore.registerRoute(child, this))
107-
routes.push(child);
108-
}, this);
109-
110-
return routes;
111-
},
112102

113103
getLocation: function () {
114104
var location = this.props.location;

modules/stores/RouteStore.js

+26-20
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ var RouteStore = {
3838
* Registers a <Route> and all of its children with the store. Also,
3939
* does some normalization and validation on route props.
4040
*/
41-
registerRoute: function (route, _parentRoute) {
42-
// Note: When route is a top-level route, _parentRoute
43-
// is actually a <Routes>, not a <Route>. We do this so
44-
// <Routes> can get a defaultRoute like <Route> does.
41+
registerRoute: function (route, parentRoute) {
42+
// Note: parentRoute may be a <Route> _or_ a <Routes>.
4543
var props = route.props;
4644

4745
invariant(
@@ -55,21 +53,21 @@ var RouteStore = {
5553

5654
if (props.path || props.name) {
5755
props.path = Path.normalize(props.path || props.name);
58-
} else if (_parentRoute && _parentRoute.props.path) {
59-
props.path = _parentRoute.props.path;
56+
} else if (parentRoute && parentRoute.props.path) {
57+
props.path = parentRoute.props.path;
6058
} else {
6159
props.path = '/';
6260
}
6361

6462
props.paramNames = Path.extractParamNames(props.path);
6563

6664
// Make sure the route's path has all params its parent needs.
67-
if (_parentRoute && Array.isArray(_parentRoute.props.paramNames)) {
68-
_parentRoute.props.paramNames.forEach(function (paramName) {
65+
if (parentRoute && Array.isArray(parentRoute.props.paramNames)) {
66+
parentRoute.props.paramNames.forEach(function (paramName) {
6967
invariant(
7068
props.paramNames.indexOf(paramName) !== -1,
7169
'The nested route path "%s" is missing the "%s" parameter of its parent path "%s"',
72-
props.path, paramName, _parentRoute.props.path
70+
props.path, paramName, parentRoute.props.path
7371
);
7472
});
7573
}
@@ -87,28 +85,36 @@ var RouteStore = {
8785
_namedRoutes[props.name] = route;
8886
}
8987

90-
if (_parentRoute && isDefault) {
88+
if (parentRoute && isDefault) {
9189
invariant(
92-
_parentRoute.props.defaultRoute == null,
90+
parentRoute.props.defaultRoute == null,
9391
'You may not have more than one <DefaultRoute> per <Route>'
9492
);
9593

96-
_parentRoute.props.defaultRoute = route;
94+
parentRoute.props.defaultRoute = route;
9795

9896
return null;
9997
}
10098

101-
// Make sure children is an array, excluding <DefaultRoute>s.
102-
var children = [];
99+
// Make sure children is an array.
100+
props.children = RouteStore.registerChildren(props.children, route);
103101

104-
React.Children.forEach(props.children, function (child) {
105-
if (child = RouteStore.registerRoute(child, route))
106-
children.push(child);
107-
});
102+
return route;
103+
}
104+
105+
/**
106+
* Registers many children routes at once, always returning an array.
107+
*/
108+
registerChildren: function (children, parentRoute) {
109+
var routes = [];
108110

109-
props.children = children;
111+
React.Children.forEach(children, function (child) {
112+
// Exclude <DefaultRoute>s.
113+
if (child = RouteStore.registerRoute(child, parentRoute))
114+
routes.push(child);
115+
});
110116

111-
return route;
117+
return routes;
112118
},
113119

114120
/**

0 commit comments

Comments
 (0)