Skip to content

Commit 1064881

Browse files
ryanflorencemjackson
authored andcommitted
[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 66030b2 commit 1064881

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
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

+15-1
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,25 @@ var Path = {
140140
return path;
141141
},
142142

143+
/**
144+
* Returns true if the given path is absolute.
145+
*/
146+
isAbsolute: function (path) {
147+
return path.charAt(0) === '/';
148+
},
149+
143150
/**
144151
* Returns a normalized version of the given path.
145152
*/
146-
normalize: function (path) {
153+
normalize: function (path, parentRoute) {
147154
return path.replace(/^\/*/, '/');
155+
},
156+
157+
/**
158+
* Joins two URL paths together.
159+
*/
160+
join: function (a, b) {
161+
return a.replace(/\/*$/, '/') + b;
148162
}
149163

150164
};

modules/helpers/makePath.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var Path = require('./Path');
88
*/
99
function makePath(to, params, query) {
1010
var path;
11-
if (to.charAt(0) === '/') {
12-
path = Path.normalize(to); // Absolute path.
11+
if (Path.isAbsolute(path)) {
12+
path = Path.normalize(to);
1313
} else {
1414
var route = RouteStore.getRouteByName(to);
1515

modules/stores/RouteStore.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ var RouteStore = {
4848
props.name || props.path
4949
);
5050

51+
var parentPath = (parentRoute && parentRoute.props.path) || '/';
52+
5153
if ((props.path || props.name) && !props.isDefault && !props.catchAll) {
52-
props.path = Path.normalize(props.path || props.name);
54+
var path = props.path || props.name;
55+
56+
// Relative paths extend their parent.
57+
if (!Path.isAbsolute(path))
58+
path = Path.join(parentPath, path);
59+
60+
props.path = Path.normalize(path);
5361
} else {
54-
props.path = (parentRoute && parentRoute.props.path) || '/';
62+
props.path = parentPath;
5563

5664
if (props.catchAll)
5765
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)