2
0
mirror of https://github.com/frappe/books.git synced 2024-12-27 12:42:44 +00:00
books/docs/client/router.md

68 lines
1.4 KiB
Markdown
Raw Normal View History

2018-01-10 12:49:52 +00:00
# Client-side Routing
The Frappe.js client comes in with a built in router, that is handles via hashing (example `#route`)
2018-01-31 10:13:33 +00:00
## Adding new route handlers
2018-01-10 12:49:52 +00:00
You can add a new route by calling `frappe.router.add`
Dynamic routes can be added by declaring each parameter as `:param` in the route string (similar to express.js)
### Example
```js
2018-01-16 06:09:17 +00:00
const Page = require('frappejs/frappe/client/view/page').Page;
2018-01-10 12:49:52 +00:00
let todo_list = new Page('ToDo List');
// make the current page active
todo_list.show();
// to do list
frappe.router.add('default', () => {
2018-02-08 12:28:51 +00:00
todo_list.show();
todo_list.list.run();
2018-01-10 12:49:52 +00:00
});
// setup todo form
frappe.router.add('edit/todo/:name', async (params) => {
app.doc = await frappe.get_doc('ToDo', params.name);
app.edit_page.show();
app.edit_page.form.use(app.doc);
});
// setup todo new
frappe.router.add('new/todo', async (params) => {
app.doc = await frappe.get_doc({doctype: 'ToDo'});
app.doc.set_name();
app.edit_page.show();
app.edit_page.form.use(app.doc, true);
});
```
2018-01-31 10:13:33 +00:00
## Setting route
You can change route with
```js
2018-02-08 12:28:51 +00:00
await frappe.router.setRoute('list', 'todo');
2018-01-31 10:13:33 +00:00
```
## Getting current route
2018-02-08 12:28:51 +00:00
`frappe.router.getRoute()` will return the current route as a list.
2018-01-31 10:13:33 +00:00
```js
2018-02-08 12:28:51 +00:00
await frappe.router.setRoute('list', 'todo');
2018-01-31 10:13:33 +00:00
// returns ['list', 'todo'];
2018-02-08 12:28:51 +00:00
route = frappe.router.getRoute();
2018-01-31 10:13:33 +00:00
```
2018-01-10 12:49:52 +00:00
## Show a route
To set a route, you can call `frappe.router.show(route_name)`
```js
frappe.router.show(window.location.hash);
```