on 06-07-2012 12:29 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.