Samples / Learn / Hash

Hash

Hash routing in mode FRESH: the URL names the CLASS, so Back and a bookmark restart the app as a new instance.

Class
z2ui5_cl_smp_app_468
Repository
abap2UI5/samples
UI5 libraries
sap.m, sap.ui.core, sap.ui.layout
Lowest UI5 release
1.71
In the browser
Runs in the playground
Controls it builds
sap.m.Button, sap.m.Input, sap.m.Label, sap.m.MessageStrip, sap.m.Page, sap.m.Shell, sap.m.Switch, sap.ui.core.mvc.View, sap.ui.layout.Grid, sap.ui.layout.form.SimpleForm
Documentation
cookbook/event_navigation/navigation/hash ↗

The class

" @keywords routing mode fresh default off navigation restart new instance nav_app_call hash_routing
" @summary Hash routing in mode FRESH: the URL names the CLASS, so Back and a bookmark restart the app as a new instance.
" @docs https://abap2ui5.github.io/docs/cookbook/event_navigation/navigation/hash
"! Hash-based app routing (UI5 Router style), mode FRESH:
"! follow_up_action( cs_event-hash_routing ) with mode FRESH makes the URL
"! mirror the running app by CLASS only ('#/app/[CLASS]'). Browser Back/Forward - and a
"! reload or a bookmark - therefore start the app FRESH: the input and the
"! counter are gone.
"!
"! Put in some state (type / raise the counter), open the detail page
"! (z2ui5_cl_smp_app_469) via client->nav_app_call( ), then press the BROWSER
"! Back button and watch this page - it comes back empty.
"!
"! z2ui5_cl_smp_app_480 is the same demo in mode KEEP, where the state survives.
"! The third mode is DEFAULT - no routing at all, the hash left untouched, the
"! way an app behaves before it ever calls hash_routing: the switch below
"! turns the routing off with it and on again with FRESH.
CLASS z2ui5_cl_smp_app_468 DEFINITION PUBLIC.

  PUBLIC SECTION.
    INTERFACES z2ui5_if_app.

    DATA input   TYPE string.
    DATA routing TYPE abap_bool.

  PROTECTED SECTION.
    DATA counter TYPE i.
    DATA client  TYPE REF TO z2ui5_if_client.

    METHODS on_event.
    METHODS view_display.

  PRIVATE SECTION.
ENDCLASS.


CLASS z2ui5_cl_smp_app_468 IMPLEMENTATION.

  METHOD z2ui5_if_app~main.

    me->client = client.

    IF client->check_on_navigated( ).
      view_display( ).

    ELSEIF client->check_on_event( ).
      on_event( ).
    ENDIF.

  ENDMETHOD.


  METHOD on_event.

    CASE client->get_event( ).

      WHEN `INC`.
        counter = counter + 1.
        view_display( ).

      WHEN `GO_DETAIL`.
        client->nav_app_call( NEW z2ui5_cl_smp_app_469( ) ).

      WHEN `ROUTING`.
        " the mode is remembered on the app, so switching it is one action:
        " DEFAULT leaves the hash alone from now on - Back/Forward leave the
        " page, the framework default - and FRESH puts the class back in
        client->follow_up_action( val   = client->cs_event-hash_routing
                                  t_arg = VALUE #( ( COND #( WHEN routing = abap_true
                                                             THEN client->cs_nav_mode-fresh
                                                             ELSE client->cs_nav_mode-default ) ) ) ).
        client->message_toast_display( COND #( WHEN routing = abap_true
                                               THEN `routing mode fresh - the URL carries the class`
                                               ELSE `routing mode default - the URL is left untouched` ) ).

    ENDCASE.

  ENDMETHOD.


  METHOD view_display.

    " configure the routing mode once - the framework remembers it on the app
    " and re-sends it whenever the frontend may not hold it (page load,
    " Back/Forward restore, navigation hops)
    IF client->check_on_init( ).
      routing = abap_true.
      client->follow_up_action( val   = client->cs_event-hash_routing
                                t_arg = VALUE #( ( client->cs_nav_mode-fresh ) ) ).
    ENDIF.

    DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
        )->ele( n = `View` ns = `mvc`
            )->a( n = `displayBlock` v = `true`
            )->a( n = `height`       v = `100%`
            )->a( n = `xmlns`        v = `sap.m`
            )->a( n = `xmlns:mvc`    v = `sap.ui.core.mvc`
            )->a( n = `xmlns:form`   v = `sap.ui.layout.form`
            )->a( n = `xmlns:layout` v = `sap.ui.layout` ).
    DATA(page) = view->ele( `Shell`
        )->ele( `Page`
            )->a( n = `title`          v = `abap2UI5 - Hash - Routing Mode FRESH (a New Instance)`
            )->a( n = `showNavButton`  b = client->check_app_prev_stack( )
            )->a( n = `navButtonPress` v = client->_event_nav_app_leave( ) ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `Add some state (type / raise the counter), open the detail page, then press your ` &&
                   `BROWSER Back button and watch this page.`
        )->a( n = `type`     v = `Information`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    DATA(form) = page->ele( n = `Grid` ns = `layout`
        )->a( n = `defaultSpan` v = `L6 M12 S12`
        )->ele( n = `content` ns = `layout`
            )->ele( n = `SimpleForm` ns = `form`
                )->a( n = `title` v = `Routing mode fresh`
                )->ele( n = `content` ns = `form` ).

    form->tag( `Label`
        )->a( n = `text` v = `1. Some state - type here` ).
    form->tag( `Input`
        )->a( n = `value` v = client->_bind( input ) ).

    form->tag( `Label`
        )->a( n = `text` v = `and raise a counter` ).
    form->tag( `Button`
        )->a( n = `press` v = client->_event( `INC` )
        )->a( n = `text`  t = |increment ({ counter })| ).

    form->tag( `Label`
        )->a( n = `text` v = `2. Navigate forward` ).
    form->tag( `Button`
        )->a( n = `press` v = client->_event( `GO_DETAIL` )
        )->a( n = `text`  v = `go to the detail page (nav_app_call)`
        )->a( n = `type`  v = `Emphasized` ).

    form->tag( `Label`
        )->a( n = `text` v = `3. Or switch the routing off again (mode default)` ).
    form->tag( `Switch`
        )->a( n = `state`         v = client->_bind( routing )
        )->a( n = `customTextOn`  v = `fresh`
        )->a( n = `customTextOff` v = `off`
        )->a( n = `change`        v = client->_event( `ROUTING` ) ).

    page->tag( `MessageStrip`
        )->a( n = `text`     v = `fresh: the URL carries the class only (#/app/<CLASS>). After the detail page, the ` &&
                   `browser Back button starts this app FRESH - input and counter are reset.`
        )->a( n = `type`     v = `Success`
        )->a( n = `showIcon` b = abap_true
        )->a( n = `class`    v = `sapUiSmallMargin` ).

    client->view_display( view->stringify( ) ).

  ENDMETHOD.

ENDCLASS.