Skip to Content
avatar image
Former Member

How to declare global variables in sapui5?

I have a controller with multiple functions and I would like to be able to crate global variables that I can use throughout all the functions. I know I can declare variables at the global scope like var x = 'something' or window.x = 'something' and just placing then before the controller declaration. But what I would like to do is be able to declare variables using my view for example in the following way:

sap.ui.define([
		'sap/ui/core/mvc/Controller',
	], function(Controller) {
	"use strict";
	//I want to be able to do something like this
       var x = this.byId('myID);
    var mainController = Controller.extend("pricingTool.controller.Main", {

           onInit : function (oEvent) {
            //set model
	var oModel = new sap.ui.model.json.JSONModel("Model/products.json");
	this.getView().setModel(oModel);
	var v1 = this.byId("gridThing");
	v1.setVisible(false);

        //and then use the variable here
        x.setAttribute("att");
        },
});        
<br>

Unfortunately this doesn't work because I receive an error saying that this.byId is not a function. Anyone knows how to accomplish this

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Nov 16, 2016 at 04:26 PM

    Eliseo,

    I recently ran into a similar issue in my own project where I needed to remember a user entered value on a different view/controller set than the one the actual submission took place on. I solved this by using SAP's provided "AppContext" to hold my value. I declared it in the app's index file like this:

    <script>
    	sap.ui.getCore().attachInit(function() {
    		new sap.m.Shell({
    			app: new sap.ui.core.ComponentContainer({
    				height : "100%",
    				name : "your.namespace"
    			})
    		}).placeAt("content");
    	});
    			
    	//set the app context object for global program data
    	sap.ui.getCore().AppContext = new Object();
    	sap.ui.getCore().AppContext.Array = [];
    	sap.ui.getCore().AppContext.BOL = "";
    </script>
    

    This was put in after the "bootstrap" script.

    Just a thought... in reading the different forums, you'll find differing opinions as to whether or not using a global variable in the MVC pattern is good practice or not. Some developers suggested creating another JSON model for the app in which you could store values. Personally, I try to avoid using the "window" global variable and stick to SAP's provided API's as much as possible.

    Good luck,

    Josh

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks Josh,

      this seems like another way to do it. I haven't tried it yet as I decided to go with an easy solution I found on another forum. This is by creating a function to initialize all the variables I need. Here is how I did it if anyone needs to see this too.

      var x, y; //declare variables with no initialization
      
      var mainController = Controller.extend("controller.Main", { 
      
             varInit: function(){ 
                 
                 //initialize variables with appropriate view elements
                 y = "something"; 
                 x = this.byId("myID"); //now I can use "this" keyword
                  },
              onInit : function (oEvent) {
                  //use variables
                  console.log(y);
                 var p = x.getAttribute("attribute");
                 x.setAttribute("att");  
             },
      });

      This might not be the best way but it's a very quick and easy way to work around this kind of issue. It seems to be working fine for me and I can use them in any function throughout my controller