Skip to Content

Create global variable in the controller

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);
		},
		
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    May 05 at 07:32 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • May 05 at 04:41 PM

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

    Add comment
    10|10000 characters needed characters exceeded

  • May 05 at 02:04 AM

    in most case, it is a bad idea.

    Add comment
    10|10000 characters needed characters exceeded