Skip to Content
avatar image
Former Member

SAP UI5 - press event of class sap.ui.commons.Button

Hi everyone!

I am trying to learn SAP UI5 myself and I have the following doubt.

If anyone could help me I would be grateful to you!

[Sorry for pasting little code here. Without showing the code I am not sure how I can explain this :-( ]

createContent : function(oController) {

console.log("createContent() of main view called...");

var exitButton = new sap.ui.commons.Button({ id : "exitButton",

text : 'Exit and kill controller',

pess : [ function(oEvent) {

this.destroy();

alert("View and Controller destroyed..."); } , alert("Hello") ] });

mainPanel.addContent(exitButton);

return mainPanel; }

When I run this "Hello" is displayed first which means before creating the exitButton, alert("Hello") statement is executed. How does it happen?

Thanks and regards,

P

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Nov 26, 2017 at 01:06 PM

    Hi Prince,

    It's one of the functionality of jQuery ie. Callback functions. Please check below links to understand callback functions.

    http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/

    https://www.w3schools.com/jquery/jquery_callback.asp

    Hope this will clear your doubts.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Irfan Gokak

      I just want to know

      "When I run this "Hello" is displayed first which means before creating the exitButton, alert("Hello") statement is executed. How does it happen?"

      The alert("Hello ") statement is supposed to execute after the exitButton object creation.

      This code is inside the view.

  • Nov 26, 2017 at 09:16 PM

    Hello,

    There is a nonsense in your code. The mistake is, the press event is not an array of function, but only one function, you put it in an array :

    ...
    Press: [ function1, function2]
    ...

    This is not correct. It should be :

    Press: function

    Regards

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      @Joseph BERTHE & Jun Wu

      Thank you very much for your responses.

      In the following code the value of 'this' is the view but then how does it invoke the 'onExit' of the controller?

      press :[function(oEvent){
      
             this.destroy();
             alert("View and Controller destroyed...");
      },this]
      
      Also when 'this' is replaced with an alert("Hello") statement "Hello" is displayed first which means before creating the object exitButton, alert("Hello") statement is executed. How does it happen?
      pess : [ function(oEvent) {
      this.destroy();
      alert("View and Controller destroyed..."); } , alert("Hello") ] });
      
      Best regards,
      P
  • Dec 06, 2017 at 04:21 AM

    Hi Prince P,

    Firstly, you should not insert your press functionality inside an array. The second alert (hello message) gets executed at beginning because of the callback functions and the SAPUI5 controller hook methods. Refer the foll. links for more information :

    https://archive.sap.com/discussions/thread/3650876

    Regards,

    Arjun Biswas

    Add comment
    10|10000 characters needed characters exceeded