Skip to content

Commit 196390f

Browse files
committed
[fixed] Make <Route name>s global, again
Fixes remix-run#865
1 parent ab802c0 commit 196390f

File tree

5 files changed

+38
-87
lines changed

5 files changed

+38
-87
lines changed

examples/dynamic-segments/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var User = React.createClass({
2525
<div className="User">
2626
<h1>User id: {userId}</h1>
2727
<ul>
28-
<li><Link to="user.task" params={{userId: userId, taskId: "foo"}}>foo task</Link></li>
29-
<li><Link to="user.task" params={{userId: userId, taskId: "bar"}}>bar task</Link></li>
28+
<li><Link to="task" params={{userId: userId, taskId: "foo"}}>foo task</Link></li>
29+
<li><Link to="task" params={{userId: userId, taskId: "bar"}}>bar task</Link></li>
3030
</ul>
3131
<RouteHandler/>
3232
</div>
@@ -53,7 +53,7 @@ var routes = (
5353
<Route path="/" handler={App}>
5454
<Route name="user" path="/user/:userId" handler={User}>
5555
<Route name="task" path="tasks/:taskId" handler={Task}/>
56-
<Redirect from="todos/:taskId" to="user.task"/>
56+
<Redirect from="todos/:taskId" to="task"/>
5757
</Route>
5858
</Route>
5959
);

examples/partial-app-loading/dashboard.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var Dashboard = React.createClass({
99
<div>
1010
<h1>Dashboard!</h1>
1111
<ul>
12-
<li><Link to="dashboard.inbox">Inbox</Link></li>
12+
<li><Link to="inbox">Inbox</Link></li>
1313
</ul>
1414
<RouteHandler/>
1515
</div>

modules/Route.js

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,102 +15,30 @@ function Route(name, path, ignoreScrollBehavior, isDefault, isNotFound, onEnter,
1515
this.handler = handler;
1616
}
1717

18-
Route.prototype.toString = function () {
19-
var string = '<Route';
20-
21-
if (this.name)
22-
string += ` name="${this.name}"`;
23-
24-
string += ` path="${this.path}">`;
25-
26-
return string;
27-
};
28-
2918
/**
3019
* Appends the given route to this route's child routes.
3120
*/
32-
Route.prototype.appendChildRoute = function (route) {
21+
Route.prototype.appendChild = function (route) {
3322
invariant(
3423
route instanceof Route,
35-
'route.appendChildRoute must use a valid Route'
24+
'route.appendChild must use a valid Route'
3625
);
3726

3827
if (!this.childRoutes)
3928
this.childRoutes = [];
4029

41-
if (route.name) {
42-
invariant(
43-
this.childRoutes.every(function (childRoute) {
44-
return childRoute.name !== route.name;
45-
}),
46-
'Route %s may not have more than one child route named "%s"',
47-
this, route.name
48-
);
49-
}
50-
5130
this.childRoutes.push(route);
5231
};
5332

54-
/**
55-
* Allows looking up a child route using a "." delimited string, e.g.:
56-
*
57-
* route.appendChildRoute(
58-
* Router.createRoute({ name: 'user' }, function () {
59-
* Router.createRoute({ name: 'new' });
60-
* })
61-
* );
62-
*
63-
* var NewUserRoute = route.lookupChildRoute('user.new');
64-
*
65-
* See also Route.findRouteByName.
66-
*/
67-
Route.prototype.lookupChildRoute = function (names) {
68-
if (!this.childRoutes)
69-
return null;
70-
71-
return Route.findRouteByName(this.childRoutes, names);
72-
};
73-
74-
/**
75-
* Searches the given array of routes and returns the route that matches
76-
* the given name. The name should be a . delimited string like "user.new"
77-
* that specifies the names of nested routes. Routes in the hierarchy that
78-
* do not have a name do not need to be specified in the search string.
79-
*
80-
* var routes = [
81-
* Router.createRoute({ name: 'user' }, function () {
82-
* Router.createRoute({ name: 'new' });
83-
* })
84-
* ];
85-
*
86-
* var NewUserRoute = Route.findRouteByName(routes, 'user.new');
87-
*/
88-
Route.findRouteByName = function (routes, names) {
89-
if (typeof names === 'string')
90-
names = names.split('.');
91-
92-
var route, foundRoute;
93-
for (var i = 0, len = routes.length; i < len; ++i) {
94-
route = routes[i];
95-
96-
if (route.name === names[0]) {
97-
if (names.length === 1)
98-
return route;
99-
100-
if (!route.childRoutes)
101-
return null;
33+
Route.prototype.toString = function () {
34+
var string = '<Route';
10235

103-
return Route.findRouteByName(route.childRoutes, names.slice(1));
104-
} else if (route.name == null) {
105-
// Transparently skip over unnamed routes in the tree.
106-
foundRoute = route.lookupChildRoute(names);
36+
if (this.name)
37+
string += ` name="${this.name}"`;
10738

108-
if (foundRoute != null)
109-
return foundRoute;
110-
}
111-
}
39+
string += ` path="${this.path}">`;
11240

113-
return null;
41+
return string;
11442
};
11543

11644
var _currentRoute;
@@ -223,7 +151,7 @@ Route.createRoute = function (options, callback) {
223151
parentRoute.notFoundRoute = route;
224152
}
225153

226-
parentRoute.appendChildRoute(route);
154+
parentRoute.appendChild(route);
227155
}
228156

229157
// Any routes created in the callback

modules/__tests__/Router-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ describe('Router.makePath', function () {
827827

828828
describe('when there is a route with the given name', function () {
829829
it('returns the correct path', function () {
830-
expect(router.makePath('home.users.user', { id: 6 })).toEqual('/home/users/6');
830+
expect(router.makePath('user', { id: 6 })).toEqual('/home/users/6');
831831
});
832832
});
833833

modules/createRouter.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ function hasMatch(routes, route, prevParams, nextParams, prevQuery, nextQuery) {
6363
});
6464
}
6565

66+
function addRoutesToNamedRoutes(routes, namedRoutes) {
67+
var route;
68+
for (var i = 0, len = routes.length; i < len; ++i) {
69+
route = routes[i];
70+
71+
if (route.name) {
72+
invariant(
73+
namedRoutes[route.name] == null,
74+
'You may not have more than one route named "%s"',
75+
route.name
76+
);
77+
78+
namedRoutes[route.name] = route;
79+
}
80+
81+
if (route.childRoutes)
82+
addRoutesToNamedRoutes(route.childRoutes, namedRoutes);
83+
}
84+
}
85+
6686
/**
6787
* Creates and returns a new router using the given options. A router
6888
* is a ReactComponent class that knows how to react to changes in the
@@ -134,6 +154,7 @@ function createRouter(options) {
134154

135155
clearAllRoutes: function () {
136156
this.cancelPendingTransition();
157+
this.namedRoutes = {};
137158
this.routes = [];
138159
},
139160

@@ -144,6 +165,8 @@ function createRouter(options) {
144165
if (isReactChildren(routes))
145166
routes = createRoutesFromReactChildren(routes);
146167

168+
addRoutesToNamedRoutes(routes, this.namedRoutes);
169+
147170
this.routes.push.apply(this.routes, routes);
148171
},
149172

@@ -174,7 +197,7 @@ function createRouter(options) {
174197
if (Path.isAbsolute(to)) {
175198
path = to;
176199
} else {
177-
var route = (to instanceof Route) ? to : Route.findRouteByName(this.routes, to);
200+
var route = (to instanceof Route) ? to : this.namedRoutes[to];
178201

179202
invariant(
180203
route instanceof Route,

0 commit comments

Comments
 (0)