Still.js Routing and Router Object
Overview
Unlike some other framerowkrs Still.js has a built-in navitation capabilities, basically it provides with a different was of addressing it from basically jumping from one component/page to another to a more robust use cases such as passing data when navigating and URL navigation. By default the navigation won't reflect the URL in the browser navigation bar, for that we need to pass the URL para as true.
All Routing capabilities in the Framework is provided by the Router Helper/Object, it taks care of data passing, handles route name and url.
Component metadata for navigation is provided through the route.map.js
file, it contains three data for each component, such metadata is added automatically when component is created using still-cli (see here), and eache component metadata has the format as follow:
-
Route name - A component's name, used for navigation, must be unique across the project (except for vendor components) and cannot be changed unless the component itself is renamed.
-
path - It's the folder path where the component is located and cannot be changed unless the component is physically moved to a different folder.
-
url - It can be used in the browser navigation bar or in a regular HTML link, it can be changed as per the dev will.
Throughout this documentation/tutorial taking into consideration the component that will be represented, the Routing file (route.map.js
), the project structure and the app-setup.js
will be set as shown bellow, in app-setup.js
we set the MainMenuComponent
as first loading component:
project-root-folder
|__ @still/
|__ app/
| |
| |__ navigation/ #-> This is component folder
| | |__ MainMenuComponent.js
| | |
| |__ person/ #-> This is component folder
| | |__ PersonForm.js
| | |__ PersonList.js
| | |
|__ config/
| |__ app-setup.js
| |__ route.map.js
|__ ... #-> Remaining files
Routes generation and management
When using still-cli to generate the components, routes are added automatically as well as the component name conflicts is checked, also, it's not recommended at all to generate components manually.
Navigation using goto in the (click)
directive event
This example takes into consideration the route mapping file and folder structure defined above.
In addition to allow component method binding, the (click)
directive also has a capability to provide navigation withouth any method binding, as only goto()
is needed to be specified, ans pass the Route name.
This is simple navigation approach, where only it's needed to go from one component/page to another, anyway, more powerfull navigation is possible using the same directive.
Passing Data from Component to Component when navigating using goto()
This example takes into consideration the route mapping file and folder structure defined above.
MainMenuComponent - 2 scenarios are in place, first (line 20) we're passing a complex data structure to another component by navigation, hence we need to assign to a variable (state or prop) in the component. The second scenario (line 23), we're just passing a string, hence we only need to enclose it in single quotation mark.
PersonList Used state variable for reactive template binding. Data is fetched using the Router helper, which requires the target object as input. stOnRender()
hook triggers this process when the component is rendering.
PersonForm Used state variable for reactive template binding. in this case, because Data is a JSON object, it's being stringified before assignment. stAfterInit()
hook triggers when the component is rendered.
Router Helper - Routing inside a Business logic/Method
This example takes into consideration the route mapping file and folder structure defined above.
MainMenuComponent - We've implemented a method which uses the Route helper for navigation through the .goto()
. Unlike the (click) directive, the goto through the Helper recieves an object as second parameter, this object has varios properties, and data can be of any type.
PersonForm Used state variable for reactive template binding. in this case, because Data is a JSON object, it's being stringified before assignment. stAfterInit()
hook triggers when the component is rendered.
Router Helper within Lone component
This example takes into consideration the route mapping file and folder structure defined above.
When it comes to Microfrontend, there might be a situation where more than one Still.js microfronted is present, for this reason, when dealing with navigation it's important for the Router to know which frontend should be affected.
To specify which microservice is triggering the navigation, we just need to pass the evt
property (line 23) in the second parameter which is assigned with serviceId from Router helpr, this id is generated and managed internally by the Router itself, we're just informing that the navigation event it triggered from such service.
ServiceId without Router Helper
When using the goto()
with the (click)
directive we don't need to inform the serviceId as it's taken care automatically by the router helper.
Making navigation reflect the browser URL in navigation Bar
As depicted above, by default the navigation won't reflect the in the browser URL, anyway, in both scenarios (using goto()
in the (click)
directive - see here) as well as (Router Helper
- see here), we can instruct the Router to put the URL by passing url parameter as true, as follow:
-
goto() with (click) directive - it will be
goto('RouteName','Data',true)
, where url param is the 3rd pararmeter, and data can be passed as null if not needed. -
Router Helper - It'll be
Router.goto('RouteName', {url: true})
, and as shown in the Router Helper example data can be passed as a property of a second parameter along with the url flag.
Using the URL defined in the route.map.js
This example takes into consideration the route mapping file and folder structure defined above.
Regular URL navigaition is in place through the component mapped URL, however, as of now it does not support data passing, this capability will be provided in the future. Follow the example:
Navigates to another component by using its url | |
---|---|
Goes back to MainMenu by using its url | |
---|---|
Navigation via URL requires the usage of the HTML link (<a>) tag, and the URL needs to be prefixed with hash (#) character when assigning the url in the href
.