Skip to content

Commit

Permalink
[changed] paths to inherit parents
Browse files Browse the repository at this point in the history
- 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 remix-run#244
  • Loading branch information
ryanflorence authored and mjackson committed Aug 29, 2014
1 parent 66030b2 commit 1064881
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/animations/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var Image = React.createClass({
var routes = (
<Routes>
<Route handler={App}>
<Route name="image" path="/:service" handler={Image} addHandlerKey={true} />
<Route name="image" path=":service" handler={Image} addHandlerKey={true} />
</Route>
</Routes>
);
Expand Down
4 changes: 2 additions & 2 deletions examples/dynamic-segments/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ var Task = React.createClass({
var routes = (
<Route handler={App}>
<Route name="user" path="/user/:userId" handler={User}>
<Route name="task" path="/user/:userId/tasks/:taskId" handler={Task}/>
<Redirect from="/user/:userId/todos/:taskId" to="task"/>
<Route name="task" path="tasks/:taskId" handler={Task}/>
<Redirect from="todos/:taskId" to="task"/>
</Route>
</Route>
);
Expand Down
16 changes: 15 additions & 1 deletion modules/helpers/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,25 @@ var Path = {
return path;
},

/**
* Returns true if the given path is absolute.
*/
isAbsolute: function (path) {
return path.charAt(0) === '/';
},

/**
* Returns a normalized version of the given path.
*/
normalize: function (path) {
normalize: function (path, parentRoute) {
return path.replace(/^\/*/, '/');
},

/**
* Joins two URL paths together.
*/
join: function (a, b) {
return a.replace(/\/*$/, '/') + b;
}

};
Expand Down
4 changes: 2 additions & 2 deletions modules/helpers/makePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var Path = require('./Path');
*/
function makePath(to, params, query) {
var path;
if (to.charAt(0) === '/') {
path = Path.normalize(to); // Absolute path.
if (Path.isAbsolute(path)) {
path = Path.normalize(to);
} else {
var route = RouteStore.getRouteByName(to);

Expand Down
12 changes: 10 additions & 2 deletions modules/stores/RouteStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ var RouteStore = {
props.name || props.path
);

var parentPath = (parentRoute && parentRoute.props.path) || '/';

if ((props.path || props.name) && !props.isDefault && !props.catchAll) {
props.path = Path.normalize(props.path || props.name);
var path = props.path || props.name;

// Relative paths extend their parent.
if (!Path.isAbsolute(path))
path = Path.join(parentPath, path);

props.path = Path.normalize(path);
} else {
props.path = (parentRoute && parentRoute.props.path) || '/';
props.path = parentPath;

if (props.catchAll)
props.path += '*';
Expand Down
25 changes: 25 additions & 0 deletions specs/RouteStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ describe('when a route is looked up by name', function () {
});

describe('when registering a route', function () {

describe('that starts with /', function() {
it('does not inherit the parent path', function() {
var child;
var route = Route({ name: 'home', handler: App },
child = Route({ path: '/foo', handler: App })
);
RouteStore.registerRoute(route);
expect(child.props.path).toEqual('/foo');
RouteStore.unregisterRoute(route);
});
});

describe('that does not start with /', function() {
it('inherits the parent path', function() {
var child;
var route = Route({ name: 'home', handler: App },
child = Route({ path: 'foo', handler: App })
);
RouteStore.registerRoute(route);
expect(child.props.path).toEqual('/home/foo');
RouteStore.unregisterRoute(route);
});
});

describe('with no handler', function () {
it('throws an Error', function () {
expect(function () {
Expand Down

0 comments on commit 1064881

Please sign in to comment.