just.Router()
Route a SPA easily.
Example
const router = new just.Router();
router.route('home', {
'pathname': '/',
'search': /\breload=([^&]+)/
}, (event, {route, data}) => {
const {action, by} = route;
let reload;
if (by === 'search') {
reload = RegExp.$1;
}
if (/init|popstate/.test(action)) {
// @TODO Call controllers.
}
});
Methods
changeState(action, url) → {boolean}
Call a window.history's function if available. Otherwise, change the current url using location.hash and prepending a hashbang (#!) to the url state.
Parameters:
Name | Type | Description |
---|---|---|
action | string | "pushState" or "replaceState". |
url | url | A same-origin url. |
Returns:
`false` if something fails, `true` otherwise.
- Type
- boolean
pushState(url) → {boolean}
Do a history.pushState calling just.Router.changeState.
Parameters:
Name | Type | Description |
---|---|---|
url | url | just.Router.changeState's url param. |
Returns:
just.Router.changeState's returned value.
- Type
- boolean
replaceState(url) → {boolean}
Do a history.replaceState calling just.Router.changeState.
Parameters:
Name | Type | Description |
---|---|---|
url | url | just.Router.changeState's url param. |
Returns:
just.Router.changeState's returned value.
- Type
- boolean
change(action, url, data, eventInit)
Do a just.Router.changeState and trigger an action if it succeds.
Parameters:
Name | Type | Description |
---|---|---|
action | string | A valid just.Router.changeState's action. |
url | url | The new url. |
data | Any | Some data. |
eventInit | CustomEventInit | Options for the event. |
push(url, data)
Trigger a "pushState" action by calling just.Router#change.
Parameters:
Name | Type | Description |
---|---|---|
url | url | just.Router#change's url argument. |
data | Any | just.Router#change's data argument. |
CustomEventInit | just.Router#change's eventInit argument. |
replace(url, data)
Trigger a "replaceState" action by calling just.Router#change.
Parameters:
Name | Type | Description |
---|---|---|
url | url | just.Router#change's url argument. |
data | Any | just.Router#change's data argument. |
CustomEventInit | just.Router#change's eventInit argument. |
route(id, path, handler, optionsnon-null)
Define a route, attach listeners (for "popstate" and custom events), and trigger an "init" event.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id | string | Some unique string to identify the current route. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
path | string or RegExp or object | A value that will match the current location. A string/RegExp is the same as passing {"pathname": string/RegExp}. An object must contain any window.location's keys, like "search", "hash", ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
handler | function | Some function that will be called when the route matches the current url. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options | object | Properties
|
trigger(action, data, eventInit)
Call a custom action on the current route by triggering a CustomEvent on each given route.
Parameters:
Name | Type | Description |
---|---|---|
action | string | Some string. |
data | Any | Data for the triggered event. |
eventInit | CustomEventInit | Options for CustomEvent's eventInit argument. |
Examples
import router from 'routes/router.js';
router.route('all', /./, (e, {route, data}) => {
const {action} = route;
if (action === 'my-action') {
console.log(`triggered ${data} on any route!`); // > "triggered my-data on any route!"
}
});
router.route('home', '/', () => {
if (action === 'my-action') {
// ignored.
}
});
router.constructor.pushState('/item');
router.trigger('my-action', 'my-data');
// index.html with "/item/a/b/c" as a url.
<a href='#close'></a>
// controllers/item.js
function closeItem ({event, target}) {
console.log("I'm closing...");
// @TODO Close.
}
export {closeItem as close}
// routes/item.js
import router from 'router.js';
import * as controller from '../controllers/item.js';
router.route('item', {
'pathname': /^\/item\//,
'hash': /^#!\/item\// // Set backwards compability.
}, (e, {route, data}) => {
if (action === 'close') {
controller.close(data);
}
});
// listeners/link.js
import router from 'routes/router.js';
// This is only for demostration purposes.
just.on(document, 'click', (event) => {
const {target} = event;
const {hash} = target;
if (hash) {
let action = hash.slice(1);
let data = {'event': e, target};
router.trigger(action, data);
}
});
// Then, click an anchor link and the corresponding controller
// will be called.
Type Definitions
route_ignore() → {boolean}
This:
{just.Router~route}
Returns:
If `true`, the route won't be called.
- Type
- boolean
route_only() → {boolean}
This:
{just.Router~route}
Returns:
If `true`, the route will be called.
- Type
- boolean