cancel
Showing results for 
Search instead for 
Did you mean: 

Declaring Global variables in SAP UI5 javascript files

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

alexanderaigner
Explorer
0 Kudos

very helpfull,

thanks!

former_member193103
Participant
0 Kudos

Hi Pritin,

I declare a global variable and want to show the data of that variable in an Input field. I am using sap.m library. Can you please show me some example how can I do this???

Thanks

Himadri

Former Member
0 Kudos

Hey Himadri,

If window.myVar is your global variable, just call the setValue(window.myVar) method of your sap.m Input control.

Caution though; if you see the need to use such global variables in an application that will eventually be in production, you might want to consider improving your design.

Regards,

Pritin

Former Member
0 Kudos

Nice,

worked like a charm.

But you say this is just one of many possible solutions... can  you show the others with the pros and cons?

Many Thanks,

Marc

Former Member
0 Kudos

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 )

Answers (2)

Answers (2)

Former Member

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

Former Member
0 Kudos

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;