cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Calling a function inside a dialog button

Former Member
0 Kudos
2,401

Hello,

I want to call a function inside a dialog after pressing on a button: It looks like this:

example: function(oEvent){
    console.log("a"); 
},


var dialog = new Dialog({
  title: 'Title',
  type: 'Message',

  content: [new Label({ 
                 text: 'Parameter 1:', labelFor: 'labelforparameter1'}),
	    new TextArea('labelforparameter1', {
		 width: '50%',
		 height: '50%',
		 placeholder: 'fill out'})],

  beginButton: new Button({
		 text: 'Download',
	         icon: 'sap-icon://download',
		  press: function () {
                        this.example(oEvent);        //Thats where I want to                     }                                   call a function	
	});
          dialog.open();
	    	
}
      

But i always get the Error Message this.example is not a function.

Both functions are defined in the same controller.

How do I solve this problem?

thanks.

Accepted Solutions (0)

Answers (6)

Answers (6)

SergioG_TX
SAP Champion
SAP Champion

the "this" keyword refers to the event since it is inside of the press event. you should put the function in the controller... then following MVC practices you could call it as: yourController.example ... otherwise, you need to do something like:

var self = this; // at the view level...

then in the press event... call it as: self.example that way you are using the right scope

dev3
Explorer
0 Kudos

tried like that but still nothing happend pls post any working secenario thanx for you support keep the good work

dev3
Explorer
0 Kudos
tried like that but still nothing happend please post a working scenario thank you so much
vsingh_mscg
Explorer
0 Kudos

add .bind(this) to your press function.

 beginButton: new Button({
		 text: 'Download',
	         icon: 'sap-icon://download',
		  press: function () {
                        this.example(oEvent);        
                  }.bind(this)
0 Kudos

Hi,

Define var that = this within function & Use that instead of this in press function like below Code

beginButton: new sap.m.Button({ text: "Save", press: function() { that.saveAction(); dialog.close(); } }),


Otherwise you can use Below method also;

var that = this;

press:[that.example,that];

-----------------

example:function(oEvent){

----------------

}

Regards,

asha

vedaradhya
Participant
0 Kudos

Hi Kerem Ünal,

try this code. i did change

you have to call function like this

press: this.onDialogBegin.bind(this)

onDialogBegin: function(oEvent) {
			console.log("a");
		},


	fnCreatDialog: function() {
			var dialog = new sap.m.Dialog({
				title: 'Title',
				type: 'Message',


				content: [new sap.m.Label({
						text: 'Parameter 1:',
						labelFor: 'labelforparameter1'
					}),
					new sap.m.TextArea('labelforparameter1', {
						width: '50%',
						height: '50%',
						placeholder: 'fill out'
					})
				],


				beginButton: new sap.m.Button({
					text: 'Download',
					icon: 'sap-icon://download',
					press: this.onDialogBegin.bind(this) //Thats where I want to                                               }                                 call a function	
				})
			});
			dialog.open();
		}

keremuenal
Advisor
Advisor
0 Kudos

thank you for your answer.

now this happens. And the dialog never opens.

vedaradhya
Participant
0 Kudos

try like this once

Can you paste above entire code in controller and use the below code to open dialog

this.fnCreatDialog();

Sharathmg
Active Contributor
0 Kudos

Just put, press: 'onDialogBegin'. This function shall be defined in the controller as separate method.

Meanwhile, try to assign functions for beforeOpen, beforeClose etc. events for the Dialog, if you want to handle them too.


keremuenal
Advisor
Advisor
0 Kudos

thank you for your answer.

Do you mean like this?

onDialogBegin: function(oEvent){
console.log("a");
},


var dialog = new Dialog({
  title: 'Title',
  type: 'Message',

  content: [new Label({ 
                 text: 'Parameter 1:', labelFor: 'labelforparameter1'}),
	    new TextArea('labelforparameter1', {
		 width: '50%',
		 height: '50%',
		 placeholder: 'fill out'})],

  beginButton: new Button({
		 text: 'Download',
	         icon: 'sap-icon://download',
		  press: 'onDialogBegin'              //Thats where I want to                                               }                                 call a function	
	});
          dialog.open();
	    	
}


If yes, unfortunately it still doesnt work. I now get the error message 'I.fFunction.call is not a function'. And if i change it to press: this.onDialogBegin(oEvent); i get the same error message as before: this.onDialogBegin is not a function.

brian_keenan
Product and Topic Expert
Product and Topic Expert
0 Kudos
beginButton: new Button({
		 text: 'Download',
	         icon: 'sap-icon://download',
		 press: this.example                                 
	});

Try it like this

keremuenal
Advisor
Advisor
0 Kudos

thank you for your answer.

i tried it like this but it skips the dialog and calls the function directly. My goal is to show a dialog and after clicking on the beginButton to call the function example.