Skip to Content
0
May 04, 2018 at 02:13 PM

Create global variable in the controller

4759 Views Last edit May 04, 2018 at 02:28 PM 2 rev

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);
		},