cancel
Showing results for 
Search instead for 
Did you mean: 

how work with global variables sapui5

Former Member
0 Kudos

hi, i hope can you help me,

If using global variables to store data is bad practice, how could I keep the user data obtained from a web service, I mean the data not as critical as the username to display it throughout the app?

I have read about sap.ui.getCore (). AppContext = new Object (); Or using a json model,

If that is the json, how would you use the code, in this way?

Controller:

Var oModelLogin = new myJSONModel ("model / UserModel.json");
OModelLogin.loadDataNew ("linkwebservice", handleSuccess, handleError, oParameters, true, 'GET') 

View:

New sap.m.Link ({
Text: "{/ UserModel> personUserName}",
Press: function () {
OController.onSendPassword ();
}
}). AddStyleClass ("sapUiTinyMarginTop")
]

Thank you very much I could give some suggestions, thanks in advance.

Former Member
0 Kudos

image console

former_member365727
Active Contributor
0 Kudos

I think issue is with 'Component.js'

init: function(){
	//Call init function of the parent
	UIComponent.prototype.init.apply(this, arguments);
	
	//Set data model on the view
	var oData = {
		recipient: {
			name: "World"
		}
	};
	
	var oModel = new JSONModel(oData);
	this.setModel(oModel, "userModel");
}

after creating JSON model it needs to be set in the component

Accepted Solutions (1)

Accepted Solutions (1)

former_member365727
Active Contributor

create a oData Model at component level which will act as a global model for all the views and controllers

Former Member
0 Kudos

Could you give me a sample code please?

I'm trying something like this

component:

init: function () {

UIComponent.prototype.init.apply(this, arguments);
var oUserModel = new myJSONModel();

this.getRouter().initialize();
}

controller:

oUserModel.loadDataNew("webService", handleSuccess, handleError,oParameters, true, 'GET')

But this would throw me: oUserModel is not defined.

former_member365727
Active Contributor
In your code scope of the variable 'oUserModel' is limited to method 'init' only and cannot be accessed from other methods.

Do you have 'Component.js'? if so use the below steps

1) Create model in 'Component.js' init method

var oUserModel = new myJSONModel();

2) Access the model from other controllers/views

var oComponent = this.getOwnerComponent();   //provides instance of Component
var oModel = oComponent.getModel();

Note: In the above code 'this' refers to current context scope, meaning if the code is used in event handler method then 'this' refers to UI control and the code will fail. In other normal methods 'this' will refer to controller instance and should work fine

Former Member
0 Kudos

Yes, in fact I'm working with the component.js, thank you very much. And if I want to have two models should I specify a name?

Var oUserModel = new myJSONModel ("model1");
Var oUserModel = new myJSONModel ("model2"); 

And access them as well?

Var oComponent = this.getOwnerComponent ();
Var oModel = oComponent.getModel ("model1");
Var oModel = oComponent.getModel ("model2");
former_member365727
Active Contributor
0 Kudos

yes, specify a name for the model, then you can have any number of models created

Var oUserModel = new myJSONModel ({} ,"model1");

// {} - data as JSON format inside brackets
//model1 - Name of the model
Former Member
0 Kudos

Thank you very much for sharing your knowledge, my friend.

Thanks.

Former Member
0 Kudos

Srikanth KV, Sorry to bother you again. but. I put the model in the component.js

Then from the controller of my view I get the model of component.js

But I get this error in the browser console

And when I try to use the myJSONModel.js loadDataNew method does not recognize it

Could you give me some suggestions please?

former_member365727
Active Contributor
0 Kudos

onValidation seems to be like an event handler....so 'this' in the method refers to UI control and doesn't have any Component which results in undefined for the statement this.getOwnerComponent()

I mentioned a note in the earlier post..

Note: In the above code 'this' refers to current context scope, meaning if the code is used in event handler method then 'this' refers to UI control and the code will fail. In other normal methods 'this' will refer to controller instance and should work fine

To resolve this kind of scenario store the controller reference in a global variable and use it in any method required..

sap.ui.define([
	"adois/ui/controller/BaseController",
], function (BaseController) {
      "use strict";

      var _oController;      //variable used to store controller reference
	
      return UIComponent.extend("adois.ui.controller.Login", {

        init: function () {
            _oController = this; //reference of controller 
            ......
            ........
       },

       onValidation: function(){
           var oComponent = _oController.getOwnerComponent();
           var oModel = oComponent.getModel("userModel");
       }
Former Member
0 Kudos

I did what he suggested but I still get the error 😕

console

Former Member
0 Kudos

https://archive.sap.com/discussions/thread/3870188

I just read that since version 1.28 of the getOwnerComponent does not offer access to the model? Is this true or is this confusing?

former_member365727
Active Contributor

which version of SAP Ui5 you are using..

also i noticed the issue in Component.js....adjust the code using below sample

init: function(){
	//Call init function of the parent
	UIComponent.prototype.init.apply(this, arguments);
	
	//Set data model on the view
	var oData = {
		recipient: {
			name: "World"
		}
	};
	
	var oModel = new JSONModel(oData);       //create model
	this.setModel(oModel, "userModel");      //set model to component
}

Setting model to component step is missing in your code

Former Member
0 Kudos

Thank you for your suggestion Has worked and I'm getting the model of the component through different views.

But now another problem arises, when I update the page in an application view the data of the model are lost, as I call them from a web service when starting the login of the application, When the user logs in, checks and queries the data through a web service, then stores the user's data in the model of the component, but then once entered into the application if a view is updated, the model is lost. How can I handle this situation, can you use Sessions on sapui5? Would you have any suggestions for this case? Thanks in advance.

Answers (0)