Skip to content

Commit f914db8

Browse files
committed
[changed] paths to inherit parents
- paths that start with `/` are absolute like they used to be - paths that don't start with `/` are now relative (meaning they inherit their parent path) - assumed `path`s from `name`s are relative closes #244
1 parent 6fdaefe commit f914db8

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

examples/animations/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var Image = React.createClass({
3636
var routes = (
3737
<Routes>
3838
<Route handler={App}>
39-
<Route name="image" path="/:service" handler={Image} addHandlerKey={true} />
39+
<Route name="image" path=":service" handler={Image} addHandlerKey={true} />
4040
</Route>
4141
</Routes>
4242
);

examples/dynamic-segments/app.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ var Task = React.createClass({
4949
var routes = (
5050
<Route handler={App}>
5151
<Route name="user" path="/user/:userId" handler={User}>
52-
<Route name="task" path="/user/:userId/tasks/:taskId" handler={Task}/>
53-
<Redirect from="/user/:userId/todos/:taskId" to="task"/>
52+
<Route name="task" path="tasks/:taskId" handler={Task}/>
53+
<Redirect from="todos/:taskId" to="task"/>
5454
</Route>
5555
</Route>
5656
);

modules/helpers/Path.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ var Path = {
143143
/**
144144
* Returns a normalized version of the given path.
145145
*/
146-
normalize: function (path) {
146+
normalize: function (path, parentRoute) {
147+
if (parentRoute && path.charAt(0) !== '/')
148+
path = parentRoute.props.path + '/' + path;
149+
147150
return path.replace(/^\/*/, '/');
148151
}
149152

modules/stores/RouteStore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var RouteStore = {
4949
);
5050

5151
if ((props.path || props.name) && !props.catchAll) {
52-
props.path = Path.normalize(props.path || props.name);
52+
props.path = Path.normalize(props.path || props.name, parentRoute);
5353
} else if (parentRoute) {
5454
// <Routes> have no path prop.
5555
props.path = parentRoute.props.path || '/';

specs/RouteStore.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ describe('when a route is looked up by name', function () {
2828
});
2929

3030
describe('when registering a route', function () {
31+
32+
describe('that starts with /', function() {
33+
it('does not inherit the parent path', function() {
34+
var child;
35+
var route = Route({ name: 'home', handler: App },
36+
child = Route({ path: '/foo', handler: App })
37+
);
38+
RouteStore.registerRoute(route);
39+
expect(child.props.path).toEqual('/foo');
40+
RouteStore.unregisterRoute(route);
41+
});
42+
});
43+
44+
describe('that does not start with /', function() {
45+
it('inherits the parent path', function() {
46+
var child;
47+
var route = Route({ name: 'home', handler: App },
48+
child = Route({ path: 'foo', handler: App })
49+
);
50+
RouteStore.registerRoute(route);
51+
expect(child.props.path).toEqual('/home/foo');
52+
RouteStore.unregisterRoute(route);
53+
});
54+
});
55+
3156
describe('with no handler', function () {
3257
it('throws an Error', function () {
3358
expect(function () {

0 commit comments

Comments
 (0)