Skip to Content
avatar image
Former Member

Declaring Global variables in SAP UI5 javascript files

Hi,

I want to create global variables in the views and controllers of SAP UI5 application.

Consider a scenario like this :

sap.ui.controller("testController", {

   loadData: function(oEvent){

           

globalVariable = oEvent.someAttribute;

   },

   onDataModelLoaded : function(data) {

alert(globalVariable );

             },

});

So I want to use this globalVariable accessed by all the functions inside the controller. But unfortunately this variable can never be accessed outside the scope of the given function.

I tried declaring the variable like:

sap.ui.controller("testController", {

globalVariable:0,

   loadData: function(oEvent){

  

globalVariable = oEvent.someAttribute;

   },

   onDataModelLoaded : function(data) {

alert(globalVariable );

    },

});

But this is useless too. I will be thankful for any help regarding this.

Best Regards,

Riswan

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • avatar image
    Former Member
    Mar 08, 2013 at 09:18 AM

    Hi Riswan,

    This is one of the possible solutions. You can attach it to the window DOM object which is available from both view and controller. Here's how you can do this :

    window.globalVariable = oEvent.someAttribute;

    You can use any variable name in this fashion, to attach it to the window.

    Do mark it as the answer if it helps!

    Regards,

    Pritin

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Does it necessary to use the Set() method and not just an attribuition (window.my_var = my_value;) ?

      It looks to be working fine across my  .conttroler.js.. (it's a question 😉 )

  • avatar image
    Former Member
    Jan 11, 2016 at 03:06 PM

    Your example is correct:

    sap.ui.controller("testController", {

    globalVariable:0,

       loadData: function(oEvent){

    globalVariable = oEvent.someAttribute;

       },

       onDataModelLoaded : function(data) {

    alert(globalVariable );

        },

    });

    BUT: you have to access globalVariable in function loadData through this.globalVariable.

    Then it works.

    Greetings, Martin

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Feb 14, 2016 at 12:29 PM

    You can create UI5 global variables using javascript namespaces.

    Name spaces starting with sap is exclusive to SAP UI5.

    you can declare them using two helper functions

    jQuery.sap.declare(sModuleName) //Case of object

    sap.ui.namespace(sNamespace)   //Case of variableExample:jQuery.sap.declare("sap.ui.sample.MyClass");sap.ui.sample.MyClass = { key1 : 'value1' }; sap.ui.namespace("sap.ui.sample.subspace");sap.ui.sample.subspace.member1 = 42;sap.ui.sample.subspace.member2 = 3.141;

    Add comment
    10|10000 characters needed characters exceeded