Skip to Content
avatar image
Former Member

Passing Parameters to Button Press Handler

Hi everyone. The following works:

var oDialog = new sap.ui.commons.Dialog();

oDialog.setTitle(sName);

               

oButton = new sap.ui.commons.Button();

oButton.setText("Close");

oButton.attachPress(HandleButtonClick);

oDialog.addButton(oButton);

                

// Open Dialog

oDialog.open();

function HandleButtonClick()

{    oDialog.close();        }

But this doesn't work when I pass the reference of oDialog

var oDialog = new sap.ui.commons.Dialog();

oDialog.setTitle(sName);

               

oButton = new sap.ui.commons.Button();

oButton.setText("Close");

oButton.attachPress(HandleButtonClick(oDialog));

oDialog.addButton(oButton);

                

// Open Dialog

oDialog.open();

function HandleButtonClick(oDialog)

{    oDialog.close();        }

Basically, to write generic functions.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 15, 2015 at 03:58 PM

    You can pass the dialog instance as a next parameter after the event listener. In the event listener, you can access the dialog instance referring to *this*.

    oButton.attachPress(HandleButtonClick , oDialog );
    
    function HandleButtonClick(){  
         this.close();       // this -> oDialog
    }
    
    Add comment
    10|10000 characters needed characters exceeded

    • attachPress event doesn't allow multiple parameters to be passed. But You can wrap all your parameters into a single Object and pass it.

      oButton.attachPress(HandleButtonClick , {param1:oDialog1, param2:oDialog2});
      
      function HandleButtonClick(){    console.log(this.param1) // would refer to the oDialog1 }

      SAPUI5 SDK - Demo Kit


  • Oct 15, 2015 at 01:27 PM

    Hi Jibran,

    Wil this help...? JS Bin - Collaborative JavaScript Debugging

    attachPress - is an event - you can invoke method here.

    But If you want to pass argument, you should make a function call in attachPress event 😊


    Thanks,

    Karthik A

    Add comment
    10|10000 characters needed characters exceeded