Skip to Content
author's profile photo Former Member
Former Member

How to define global variables in new UI5 project

What is the suggested way to define global variable in new UI5 webide project.

sap.ui.define([ 
"sap/ui/core/mvc/Controller" 
], function(Controller) { 
"use strict";
return Controller.extend("test.controller.View1", 
{
oView: null, 
onInit: function() { 
oView = this.getView(); 
},
});
}); 

When defined as above , i am facing an error that oView is not defined.

I have another ui5 project where controller was defined in a different way and working fine.

sap.ui.controller("test.view.main", 
{
oView: null, 
onInit: function() { 
oView = this.getView(); 
},

Why does the way controllers defined for each version. Its getting tough to catch up the pace the changes are being made to project definitions at regular intervals.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Posted on Aug 14, 2016 at 01:59 AM

    Hi Michael,

    The first option should work fine. I am not able to show on Web IDE, but here is a JS Bin: JS Bin - Collaborative JavaScript Debugging

    Select any item to see view ID in alert.

    Regards,

    Sai.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 14, 2016 at 03:14 AM

    usually it is not good idea. don't do it.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 15, 2016 at 04:58 AM

    Hi Micheal Bielawa,

    Its not good idea to use global variables in your application, but still in your code you used use strict

    keyword because of that its not allowing global variable declaration. you can remove that keyword and declare it nomally.

    or else you can store your variable in window object (ex: window.name) or parent (ex: parent.name)

    if your using "use strict".

    sap.ui.define([

    "sap/ui/core/mvc/Controller"

    ],

    function(Controller) {

    "use strict";

    return Controller.extend(

    "test.controller.View1",

    {

    oView:

    null,

    onInit:

    function() {


    window.oView = this.getView();


    or


    parent.oView = this.getView();

    },

    });

    });

    Thanks & Regards

    Venkat

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.