diff --git a/modules/components/Routes.js b/modules/components/Routes.js index c279a4fc9d..7d87437e78 100644 --- a/modules/components/Routes.js +++ b/modules/components/Routes.js @@ -95,20 +95,10 @@ var Routes = React.createClass({ getInitialState: function () { return { - routes: this.getRoutes() + routes: RouteStore.registerChildren(this.props.children, this) }; }, - getRoutes: function () { - var routes = []; - - React.Children.forEach(this.props.children, function (child) { - if (child = RouteStore.registerRoute(child, this)) - routes.push(child); - }, this); - - return routes; - }, getLocation: function () { var location = this.props.location; diff --git a/modules/stores/RouteStore.js b/modules/stores/RouteStore.js index 7525ed5304..330a44d8a7 100644 --- a/modules/stores/RouteStore.js +++ b/modules/stores/RouteStore.js @@ -38,10 +38,8 @@ var RouteStore = { * Registers a and all of its children with the store. Also, * does some normalization and validation on route props. */ - registerRoute: function (route, _parentRoute) { - // Note: When route is a top-level route, _parentRoute - // is actually a , not a . We do this so - // can get a defaultRoute like does. + registerRoute: function (route, parentRoute) { + // Note: parentRoute may be a _or_ a . var props = route.props; invariant( @@ -55,8 +53,8 @@ var RouteStore = { if (props.path || props.name) { props.path = Path.normalize(props.path || props.name); - } else if (_parentRoute && _parentRoute.props.path) { - props.path = _parentRoute.props.path; + } else if (parentRoute && parentRoute.props.path) { + props.path = parentRoute.props.path; } else { props.path = '/'; } @@ -64,12 +62,12 @@ var RouteStore = { props.paramNames = Path.extractParamNames(props.path); // Make sure the route's path has all params its parent needs. - if (_parentRoute && Array.isArray(_parentRoute.props.paramNames)) { - _parentRoute.props.paramNames.forEach(function (paramName) { + if (parentRoute && Array.isArray(parentRoute.props.paramNames)) { + parentRoute.props.paramNames.forEach(function (paramName) { invariant( props.paramNames.indexOf(paramName) !== -1, 'The nested route path "%s" is missing the "%s" parameter of its parent path "%s"', - props.path, paramName, _parentRoute.props.path + props.path, paramName, parentRoute.props.path ); }); } @@ -87,28 +85,36 @@ var RouteStore = { _namedRoutes[props.name] = route; } - if (_parentRoute && isDefault) { + if (parentRoute && isDefault) { invariant( - _parentRoute.props.defaultRoute == null, + parentRoute.props.defaultRoute == null, 'You may not have more than one per ' ); - _parentRoute.props.defaultRoute = route; + parentRoute.props.defaultRoute = route; return null; } - // Make sure children is an array, excluding s. - var children = []; + // Make sure children is an array. + props.children = RouteStore.registerChildren(props.children, route); - React.Children.forEach(props.children, function (child) { - if (child = RouteStore.registerRoute(child, route)) - children.push(child); - }); + return route; + } + + /** + * Registers many children routes at once, always returning an array. + */ + registerChildren: function (children, parentRoute) { + var routes = []; - props.children = children; + React.Children.forEach(children, function (child) { + // Exclude s. + if (child = RouteStore.registerRoute(child, parentRoute)) + routes.push(child); + }); - return route; + return routes; }, /**