Skip to content
This repository was archived by the owner on Oct 27, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ Pass a rails-style path and an object containing values for any route parameters

Match a literal `path` (no colons) against all routes and dispatch the matching action, passing in route parameters. `path` defaults to `window.location.hash` with leading and trailing slashes stripped out.

## `redirect`

Redirect the user to a different route instead of dispatching an action. This is useful anytime a
redirects is desired, such as an old route being deprecated.

```JavaScript
import createRouter, { redirect } from '@density/conduit';
import store from './store';

const router = createRouter(store);
router.addRoute('foo/bar', redirect('hello/world'));
router.addRoute('hello/world', () => ({type: "NAVIGATE_TO_HELLO_WORLD"}) );
```

## Development

Expand Down
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,24 @@ export function handle(routes, store, path) {
path = path || window.location.hash.slice(1).replace(/(^\/|\/$)/, '');
var route = checkPath(routes, path);
if (route) {
var params = route.regexp.exec(path).slice(1);
store.dispatch(route.action.apply(route, params));
const params = route.regexp.exec(path).slice(1);
const action = route.action.apply(route, params);
if (action) {
store.dispatch(action);
}
return true;
} else {
console.warn('Route to ' + path + ' not found!');
return false
}
}

export function redirect(url) {
return () => {
window.location.href = url;
}
}

// Programmatically "navigate" (just sets window.location.hash)
export function navigate(routes, path, params) {
var route = checkPath(routes, path);
Expand Down