Skip to Content
avatar image
Former Member

SAPUI5 variables scopes

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Oct 19, 2015 at 03:49 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 19, 2015 at 03:33 AM

    >>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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 18, 2015 at 06:01 PM

    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:

    Javascript Tutorial

    Thanks,

    Karthik A

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 18, 2015 at 07:22 PM

    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

    Add comment
    10|10000 characters needed characters exceeded