Skip to content

Commit 11171da

Browse files
committed
[IMP] developer: add section on router service
closes #1292 Signed-off-by: Géry Debongnie (ged) <[email protected]>
1 parent d551193 commit 11171da

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

content/developer/reference/frontend/services.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Reference List
122122
- display graphical effects
123123
* - :ref:`notification <frontend/services/notification>`
124124
- display notifications
125+
* - :ref:`router <frontend/services/router>`
126+
- manage the browser url
125127
* - :ref:`rpc <frontend/services/rpc>`
126128
- send requests to the server
127129
* - :ref:`title <frontend/services/title>`
@@ -455,6 +457,96 @@ A notification that closes after a second:
455457
const close = notificationService.add("I will be quickly closed");
456458
setTimeout(close, 1000);
457459
460+
.. _frontend/services/router:
461+
462+
Router Service
463+
--------------
464+
465+
Overview
466+
~~~~~~~~
467+
468+
- Technical name: `router`
469+
- Dependencies: none
470+
471+
The `router` service provides three features:
472+
473+
* information about the current route
474+
* a way for the application to update the url, depending on its state
475+
* listens to every hash change, and notifies the rest of the application
476+
477+
API
478+
~~~
479+
480+
.. js:data:: current
481+
:noindex:
482+
483+
The current route can be accessed with the ``current`` key. It is an object
484+
with the following information:
485+
486+
* `pathname (string)`: the path for the current location (most likely `/web` )
487+
* `search (object)`: a dictionary mapping each search keyword (the querystring)
488+
from the url to its value. An empty string is the value if no value was
489+
explicitely given
490+
* `hash (object)`: same as above, but for values described in the hash.
491+
492+
For example:
493+
494+
.. code-block:: javascript
495+
496+
// url = /web?debug=assets#action=123&owl&menu_id=174
497+
const { pathname, search, hash } = env.services.router.current;
498+
console.log(pathname); // /web
499+
console.log(search); // { debug="assets" }
500+
console.log(hash); // { action:123, owl: "", menu_id: 174 }
501+
502+
Updating the URL is done with the `pushState` method:
503+
504+
.. js:function:: pushState(hash: object[, replace?: boolean])
505+
506+
:param Object hash: object containing a mapping from some keys to some values
507+
:param boolean replace: if true, the url will be replaced, otherwise only
508+
key/value pairs from the `hash` will be updated.
509+
510+
Updates the URL with each key/value pair from the `hash` object. If a value is
511+
set to an empty string, the key is added to the url without any corresponding
512+
value.
513+
514+
If true, the `replace` argument tells the router that the url hash should be
515+
completely replaced (so values not present in the `hash` object will be removed).
516+
517+
This method call does not reload the page. It also does not trigger a
518+
`hashchange` event, nor a `ROUTE_CHANGE` in the :ref:`main bus <frontend/framework/bus>`.
519+
This is because this method is intended to only updates the url. The code calling
520+
this method has the responsibility to make sure that the screen is updated as
521+
well.
522+
523+
For example:
524+
525+
.. code-block:: javascript
526+
527+
// url = /web#action_id=123
528+
routerService.pushState({ menu_id: 321 });
529+
// url is now /web#action_id=123&menu_id=321
530+
routerService.pushState({ yipyip: "" }, replace: true);
531+
// url is now /web#yipyip
532+
533+
534+
Finally, the `redirect` method will redirect the browser to a specified url:
535+
536+
.. js:function:: redirect(url[, wait])
537+
538+
:param string url: a valid url
539+
:param boolean wait: if true, wait for the server to be ready, and redirect after
540+
541+
Redirect the browser to `url`. This method reloads the page. The `wait`
542+
argument is rarely used: it is useful in some cases where we know that the
543+
server will be unavailable for a short duration, typically just after an addon
544+
update or install operation.
545+
546+
.. note::
547+
The router service emits a `ROUTE_CHANGE` event on the :ref:`main bus <frontend/framework/bus>`
548+
whenever the current route has changed.
549+
458550
.. _frontend/services/rpc:
459551

460552
RPC service

0 commit comments

Comments
 (0)