Losing my global variables when load/unload module

  • Jan Schroeder
        Has successfully completed the online course Intermediate (200)
        Has successfully completed the online course Advanced (300)
        Has successfully completed the online course Basics (100)
        Has successfully completed the online course Advanced (310)
        Has successfully completed the online course Advanced (320)
      1 year ago #40645

      Hello dear community,
      I currently have the problem that the global variables are “deleted” when I go through the module load/unload mechanism.
      Is there any setting that needs to be configured differently?

      The content of
      sap.ui.getCore().getModel(“variableHolder”).getProperty(“/”)
      is just an empty array after I am back in my app.

      Best regards
      Jan Schroeder

      • This topic was modified 1 year ago by Jan Schroeder.
      Andreas Dallner
          Has successfully completed the online course Introduction
          Has successfully completed the online course Intermediate (200)
          Has successfully completed the online course Advanced (300)
          Has successfully completed the online course Basics (100)
          Has successfully completed the online course Advanced (310)
          Has successfully completed the online course Advanced (320)
          Has successfully completed the Intermediate Certification
          Has successfully completed the Advanced Certification
        1 year ago #40670
        Up
        1
        Down
        ::

        Hi Jan,

        the variables of your module are stored in module context -> If you unload the module the context is destroyed as well. Values are stored in sap.ui.getCore().byId(“MODULENAME”).getModel(“variableHolder”).getProperty(“/”).

        If you want to keep the values in app context you have to send the values via interface to the app and store it in the apps variables.

        We do not recommend to reading/write data directly to the apps variable holder to have consistent api usage via the modules interfaces.

        Kind regards,
        Andreas

        • This reply was modified 1 year ago by Vitali Kurdasov. Reason: replaced "getId(" with "byId("
        Vitali Kurdasov
            Has successfully completed the online course Intermediate (200)
            Has successfully completed the online course Basics (100)
            Has successfully completed the online course Advanced (310)
            Has successfully completed the Intermediate Certification
          1 year ago #40676
          Up
          1
          Down
          ::

          In addition to the previous answer, whom can get access to the global variables of the application even if a module is loaded on the current scren with the following call:

          sap.ui.getCore().byId(“Application”).getModel(“variableHolder”).getProperty(“/”)

          Jan Schroeder
              Has successfully completed the online course Intermediate (200)
              Has successfully completed the online course Advanced (300)
              Has successfully completed the online course Basics (100)
              Has successfully completed the online course Advanced (310)
              Has successfully completed the online course Advanced (320)
            1 year ago #40678
            Up
            0
            Down
            ::

            Thank you for your answers. I hope I understand it now.

            So far I have accessed the global variables via
            sap.ui.getCore().getModel(“variableHolder”).getProperty(“/”)

            This should do the trick for me
            sap.ui.getCore().byId(“Application”).getModel(“variableHolder”).getProperty(“/”)

            Vitali Kurdasov
                Has successfully completed the online course Intermediate (200)
                Has successfully completed the online course Basics (100)
                Has successfully completed the online course Advanced (310)
                Has successfully completed the Intermediate Certification
              1 year ago #40679
              Up
              0
              Down
              ::

              Hi,

              I just would like to add the best practice:

              It has certain advantages if you only use the modul-interfaces to get or set variables from the application to the module. The big advantage with this way is that you have a predefined and controlled way to access a specific variable inside a module. An access to those module-variables with the above mentioned “sap.ui.getCore()…”-call could lead to uncontrolled side-effects which makes it very hard to debug if needed.

            Viewing 5 posts - 1 through 5 (of 5 total)

            You must be logged in to reply to this topic.