Skip to Content
0

how work with global variables sapui5

Mar 14, 2017 at 06:09 PM

536

avatar image

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Srikanth KV Mar 14, 2017 at 06:17 PM
1

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

Show 11 Share
10 |10000 characters needed characters left characters exceeded

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.

0
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

1

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");
0

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
0

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

Thanks.

0

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?

componentge.png (18.0 kB)
controller.png (55.5 kB)
console.png (133.1 kB)
console2.png (125.2 kB)
0

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");
       }
0

I did what he suggested but I still get the error :/

console

erro.png (88.9 kB)
0

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?

0

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

1

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.

0