on 10-18-2015 6:37 PM
Hi all,
I am new to sapui5 development and followed the tutorials on sapui5 developer guide. I'm not an experienced developer but did some development in the past on iOS (xcode). What still confuses me on the sapui5 development is variable scoping. In xcode I am used to the fact that all variables I declare inside an method or function have a local scope and when I want to use an variable through the whole controller file I have to declare it inside the "viewController" class but outside any function itself.
My question is if there is something like that in SAPUI5 as well? is it possible to define a variable inside a controller file which you can use in all methods defined in that controller file? I know you have to be careful with global variables but sometimes it might still be useful?
Kind Regards,
Nico van der Linden
Hi Linden,
You mean to say, you wanted to have a Global Variable in the controller? where you can use the same at multiple places. Yes, there are ways to do that. You can define the variables in onInit life cycle as
this.oGlobalValue = [];
window.oGlobalValue = [];
Above can also be define after the controller function.
But, In general using global variables is not a good practice. Instead, why don't use modeling concept of SAPUI5? Something like below -
sap.ui.define([
'jquery.sap.global',
'sap/ui/model/json/JSONModel',
'sap/ui/core/mvc/Controller'
], function(jQuery, JSONModel, Controller) {
"use strict";
var PageController = Controller.extend("demo.FirstPage", {
onInit: function (oEvent) {
var oModel = new JSONModel();
sap.ui.getCore().setModel(oModel);
},
onItemPress: function(oEvent){
var oValue = oEvent.getParameter("newValue");
sap.ui.getCore().getModel().setProperty("/oSelectedValue",oValue); //Set the Property Value
}
onSearch: function(oEvent){
var oSearchValue = oEvent.getParameter("searchValue");
var oSelectedValue = sap.ui.getCore().getModel().getProperty("/oSelectedValue"); //Get the Property Value
if(oSearchValue === oSelectedValue){
sap.ui.getCore().getModel().setProperty("/oSelectedValue",oValue); //Overwrite property value (or) set the property value again
}
}
});
return PageController;
});
if you check the above sample, oSelectedValue holds a value where I wanted to use in different functions. I can set the property, get the property and overwrite the property as well. Try something like this. If this is not what you are expecting, can you please elaborate the scenario?
Regards,
Sai Vellanki.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
>>is it possible to define a variable inside a controller file which you can use in all methods defined in that controller file?
just use the model in core scope
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nico,
JavaScript implements scope slightly different to other languages, I would recommend reading the following to understand scope and closures.
You-Dont-Know-JS/scope & closures at master · getify/You-Dont-Know-JS · GitHub
Jason
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Van den linder,
SAPUi5 development - Same as JS concepts.
1. Yah, if you declare variable inside functions, you can't able to access those variable outside.
For ex:
Controller Js methods
HandlePress: function(){
Var hai = '9';
}
//global access
Var hai = '1':
HandleSelect : function(){
Alert(hai)
}
FYI:
Thanks,
Karthik A
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.