Skip to content

Commit

Permalink
[added] RouteStore#registerChildren
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Aug 18, 2014
1 parent a030648 commit 283d3f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
12 changes: 1 addition & 11 deletions modules/components/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 26 additions & 20 deletions modules/stores/RouteStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ var RouteStore = {
* Registers a <Route> 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 <Routes>, not a <Route>. We do this so
// <Routes> can get a defaultRoute like <Route> does.
registerRoute: function (route, parentRoute) {
// Note: parentRoute may be a <Route> _or_ a <Routes>.
var props = route.props;

invariant(
Expand All @@ -55,21 +53,21 @@ 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 = '/';
}

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
);
});
}
Expand All @@ -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 <DefaultRoute> per <Route>'
);

_parentRoute.props.defaultRoute = route;
parentRoute.props.defaultRoute = route;

return null;
}

// Make sure children is an array, excluding <DefaultRoute>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 <DefaultRoute>s.
if (child = RouteStore.registerRoute(child, parentRoute))
routes.push(child);
});

return route;
return routes;
},

/**
Expand Down

0 comments on commit 283d3f6

Please sign in to comment.