Router
Cx features built-in routing functionality. This enables building complex single page applications and retain classic URL based navigation. There are a few classes used to implement routing and navigation:
- Url
- Route
- Sandbox
- History
- Link
Both hash-based and pushState based navigation modes are supported.
Url
import {Url} from 'cx/ui';The Url helper class offers methods for working with url paths.
It's crucial to initialize the base path before using other methods.
Default base path is set to / which works only if the application is not
hosted in a subdirectory. Otherwise, call Url.setBase('/path-to-the-app/').
| Methods |
|---|
Url.setBase(base)Sets the base path of the application. |
Url.isLocal(path)Checks if the given path is local. |
Url.resolve(path)Resolves |
Url.setBaseFromScript(scriptPath)Sets the base path of the application by examining DOM |
Url.unresolve(path)Takes a given relative or absolute path and returns tilde based path. |
Url.setBase('/docs/');
Url.setBaseFromScript('~/app.js');
Url.isLocal('/docs/'); // true
Url.isLocal('/app/'); // false
Url.resolve('~/page'); // /docs/page
Url.unresolve('/docs/page'); // ~/page
Url.unresolve('https://cxjs.io/docs/page'); // ~/pageimport {Route} from 'cx/widgets';The Route widget is a pure container element which renders only if current url matches the assigned route.
| Property | Description | Type |
|---|---|---|
params | Params binding. Matched route parameters will be stored inside. | string |
route | Target route, e.g. | string |
url | Url binding. Bind this to the global | string |
prefix | Match route even if given | boolean |
<Route url-bind="url" route="~/about">
About
</Route>
<Route url-bind="url" route="~/intro" prefix>
Intro
</Route>
<Route url-bind="url" route="+/nested">
Nested (~/intro/nested)
</Route>Routes support parameters, splats and optional parts. For more details, check the route-parser library.
Redirect Routes
import {RedirectRoute} from 'cx/widgets';Redirect routes redirect to another page when matched.
<RedirectRoute url-bind="url" route="~/" redirect="~/intro/about"/>Redirect routes use the replaceState method of the History interface, so navigating back after redirection
will not return to the redirected page.
<Sandbox key-bind="url" storage-bind="pages">
<Route url-bind="url" route="~/about">
<TextField value-bind="$page.text" />
</Route>
</Sandbox>import {History} from 'cx/ui';The History class is used for working with HTML5 pushState navigation.
| Methods |
|---|
History.addNavigateConfirmation(callback, permanent)Instructs the router to execute the given callback to confirm leaving the current page. Return value should
be a boolean value or a promise. The callback is executed only for the current page, unless |
History.connect(store, bind)Initializes a link between browser's location and store variable pointed by the |
History.pushState(state, title, url)Performs navigation to a new location pointed by the |
History.reloadOnNextChange()Instructs the router to reload the page on next navigation. This is commonly used with service workers. |
History.replaceState(state, title, url)Performs navigation to a new location pointed by the |
History.subscribe(callback)Subscribe to location changes (navigation). Useful for setting up page tracking (e.g. Google Analytics) on CxJS apps. The function returns a function which can be used to unsubscribe. |
History.connect(store, 'url');
History.subscribe(url => {
if (window.ga) {
ga('set', 'page', url);
ga('send', 'pageview');
}
});Please note that fallback mechanism for browsers which do not support
pushStatenavigation is standard navigation (full page load).
The following example illustrates the use of History.addNavigateConfirmation to ask the user for confirmation
before leaving the page.
<Button
onClick={(e, {store}) => {
History.addNavigateConfirmation(
url => MsgBox
.yesNo('Are you sure you want to leave this page?')
.then(result => result == 'yes')
);
store.set('$page.confirmation', true);
}}
text="Add Confirmation"
/>
<p ws visible-expr="!!{$page.confirmation}">
Good. Now try navigating to <a href="~/concepts/outer-layouts">some other page</a>.
</p>import {Link} from 'cx/widgets';The Link widget is a replacement for anchor elements when pushState based navigation is used.
| Property | Description | Type |
|---|---|---|
activeClass | Additional CSS class to be applied to the element when it's active. | string/object |
activeStyle | Additional CSS style to be applied to the element when it's active. | string/object |
confirm | Confirmation text or configuration object. See MsgBox.yesNo for more details. | string/object |
enabled | Set to | boolean |
href | Url to the link's target location. The link should start with | string |
inactiveClass | Additional CSS class to be applied to the element when it's not active. | string/object |
inactiveStyle | Additional CSS style to be applied to the element when it's not active. | string/object |
match | Accepted values are | string |
pressed | If | boolean |
target | This attribute specifies where to open the linked document. | string |
url | Binding to the current url location in the store. If | string |
autoFocus | Set to | boolean |
baseClass | Base CSS class to be applied to the element. Default is 'link'. | string |
class | Additional CSS classes to be applied to the element. If an object is provided, all keys with a "truthy" value will be added to the CSS class list. | string/object |
disabled | Set to | boolean |
dismiss | If set to | boolean |
focusOnMouseDown | Determines if button should receive focus on | boolean |
icon | Name of the icon to be put on the left side of the button. | string |
innerHtml | HTML to be injected into the element. | string |
items | List of child elements. | array |
layout | Define an inner layout. | string/object |
mod | Appearance modifier. Cx ships with | string/array |
outerLayout | Defines the outer layout which wraps the widget. | widget |
plainText | Set to | boolean |
preserveWhitespace | Keep whitespace in text based children. Default is | boolean |
putInto | Used with outer layouts. Specifies the name of the content placeholder which should render the widget. | string |
style | Style object applied to the wrapper div. Used for setting the dimensions of the element. | string/object |
submit | Add | boolean |
tag | HTML tag to be used. Default is | string |
text | Inner text contents. | string |
tooltip | Tooltip configuration. For more info see Tooltips. | string/object |
trimWhitespace | Remove all whitespace in text based children. Default is | boolean |
vdomKey | Key that will be used as the key when rendering the React component. | string |
visible | Visibility of the widget. Defaults to | boolean |
<Link href="~/about">About<Link>