A simple client router
It also works with local files paths (file:///)
- Initialize the router at application startup using: router.init(setup)
- Trigger a new route using: kiss.router.navigateTo(newRoute)
- The router also observes url hash changes and automatically triggers new routes accordingly
When initializing the router, you can optionally define what to do:
- before a routing event occurs, using "beforeRouting" callback
- after the routing event occurred, using "afterRouting" callback
// 1) Init your app router:
kiss.router.init({
beforeRouting: async () => { await doStuff() },
afterRouting: async () => { await doOtherStuff() }
})
// 2) Use it to change your application route:
const newApplicationRoute = {ui: "homepage", applicationId: "123", viewId: "456"}
kiss.router.navigateTo(newApplicationRoute)
// 3) Get the current application route by reading the url hash:
const currentApplicationRoute = kiss.router.getRoute()
Members
# static hooks
Hooks to modify the router behavior before and after routing
# static publicRoutes
Default list of public routes which doesn't require authentication.
Add custom public routes using addPublicRoutes([...]) method.
By default, the following routes are public:
- authentication-login
- authentication-register
- authentication-reset-password
- authentication-error
Methods
# static addPublicRoutes(publicRoutes)
Add some public routes
Parameters:
Name | Type | Description |
---|---|---|
publicRoutes |
Array.<string>
|
# static getRoute() → {object}
Get the current application route from the url hash.
For example:
- if current url is: http://.../...#ui=homepage&applicationId=123&viewId=456
- the output is: {ui: "homepage", applicationId: "123", viewId: "456"}
object
# static init(setup)
Init the router
It will observe any url hash change, which will:
- perform a custom action before triggering the new route
- perform a custom action after the routing
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
setup |
object
|
The router setup, containing the 2 methods: |
|
publicRoutes |
Array.<string>
|
<optional> |
Define public routes (skip login) |
beforeRouting |
function
|
<optional> |
Method to execute before routing |
afterRouting |
function
|
<optional> |
Method to execute after routing |
# static isPublicRoute() → {boolean}
Check if the current route (given by the ui parameter) is public
boolean
# static navigateTo(newRoute, resetopt)
Navigate to a new hash It indirectly triggers the new route by dispatching the window's hashchange event.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
newRoute |
object
|
string
|
||
reset |
boolean
|
<optional> |
Set to true to reset the previous route before routing to a new one |
Example
// Using an object
const newRoute = {ui: "homepage", applicationId: "123", viewId: "456"}
kiss.router.navigateTo(newRoute)
// Using a string
kiss.router.navigateTo("home-start") // Is equivalent to: kiss.router.navigateTo({ui: "home-start"})
# static setPublicRoutes(publicRoutes)
Set the public routes
Parameters:
Name | Type | Description |
---|---|---|
publicRoutes |
Array.<string>
|
# static updateUrlHash(newRoute, resetopt)
Update URL hash according to new route params.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
newRoute |
object
|
||
reset |
boolean
|
<optional> |
True to reset the current hash |
Example
kiss.router.updateUrlHash({chapter: 10, section: 2}, true)