Skip to Content
0

Create global variable in the controller

May 04 at 02:13 PM

295

avatar image

Hi, i wanto to create a global variable but just in my controller so i don't have to create my variables again in other function

this is my code:

onInit : function() {
			
		},
		
		onBuscar : function() {
			var oData = new JSONModel;
			oData.rut		= this.byId("irut").getValue();
			oData.nombres 	= this.byId("iNombres").getValue();
			oData.apellidos = this.byId("iApellidos").getValue();
			oData.calle 	= this.byId("iDir1").getValue();
			oData.numero 	= this.byId("iDir2").getValue();
			oData.casa 		= this.byId("iDir3").getValue();
			oData.comuna 	= this.byId("iDir4").getValue();
			oData.tlfFijo 	= this.byId("iTlf1").getValue();
			oData.tlfMovil 	= this.byId("iTlf2").getValue();
			oData.mail 		= this.byId("iMail").getValue();
			oData.pais 		= this.byId("iPais").getValue();
			
			MessageToast.show(oData.rut);
		},
		
		onModif : function() {
			
		},
		
		onCrear : function() {
			
		}

i tried to create in the oninit function but get an error of undefined variable i tried like this

onInit : function() {

this.oData = new JSONModel;

oData.rut = this.byId("irut").getValue;

},
onBuscar : function() {MessageToast.show(oData.rut);
}

any suggestion? thanks!

UPDATE 1

i tried this but when i print my values are empty :/ unless that my inputs are editable false

onInit : function() {
			$.oData = new JSONModel;
			$.oData.rut		= this.byId("irut").getValue();
			$.oData.nombres 	= this.byId("iNombres").getValue();
			$.oData.apellidos = this.byId("iApellidos").getValue();
			$.oData.calle 	= this.byId("iDir1").getValue();
			$.oData.numero 	= this.byId("iDir2").getValue();
			$.oData.casa 		= this.byId("iDir3").getValue();
			$.oData.comuna 	= this.byId("iDir4").getValue();
			$.oData.tlfFijo 	= this.byId("iTlf1").getValue();
			$.oData.tlfMovil 	= this.byId("iTlf2").getValue();
			$.oData.mail 		= this.byId("iMail").getValue();
			$.oData.pais 		= this.byId("iPais").getValue();
		},
		
		onBuscar : function() {
					
			MessageToast.show($.oData.rut);
		},
		
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Enric Castella Gonzalez May 05 at 07:32 PM
1

I recomend use model to binding data.

But if you desided use this, you must to declare variable in controller, before onInit(). Also, you doesn't need use this:

this.oData =new JSONModel;

oData.rut = this.byId("irut").getValue;

because you modify json model standard structure to get value.

Check the guide to understand how to use the MVC model

https://sapui5.hana.ondemand.com/#/topic/3da5f4be63264db99f2e5b04c5e853db

Show 1 Share
10 |10000 characters needed characters left characters exceeded
0
BobuGeorge P May 05 at 04:41 PM
2

Its not a good practice. But, you can use this.myVar = 'value1'; and use it inside any function.

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

Thanks, so what do you recommend to avoid rewrite same code?

0
Jun Wu May 05 at 02:04 AM
1

in most case, it is a bad idea.

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

Thanks! Jun but i want to avoid rewrite same code

0