cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member484715
Contributor
0 Kudos

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

Joseph_BERTHE
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Joseph

Thank you so much.

I just want to display the order of the life span of view and controller. Could you please tell me how I can achieve this?

Best regards,

P

Joseph_BERTHE
Active Contributor
0 Kudos

Look at this Link

Former Member
0 Kudos

Dear Joseph,

I was following the same. For press event there is an array consisting of a function and 'this'. You have pointed out that press event is not array of function it is only one function. Please clarify this. Thanks and regards, P

press :[function(oEvent){

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


Former Member
0 Kudos

Any help would be appreciated...

Joseph_BERTHE
Active Contributor
0 Kudos

Ok I see now 🙂

The way in the example is probably an old fashion way to code with jQuery.

Instead of using a array do this :

Press: function (oevent){...}.bind(this)
junwu
Active Contributor
0 Kudos

array is the very standard UI5 way to designate the "this"

Former Member
0 Kudos

@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
irfan_gokak
Contributor
0 Kudos

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.

Former Member
0 Kudos

Hi Irfan,

Thank you for your reply.

Is there any way to create the exitButton first then display alert("Hello")?

"press" member of the function(constructor) 'sap.ui.commons.Button' is an array of 2 items which are 1. function(oEvent) and 2. alert("Hello")

Also there is no effects to take place for the delay to execute alert("Hello")

I have gone through the links which you have provided and understand what callback function is however I am not able to understand this scenario. If you can explain bit it will be a great help.

Best regards,

P

irfan_gokak
Contributor
0 Kudos

Please tell me what you're trying to do with this code? what is your scenario? and are you doing it in controller?

Former Member
0 Kudos

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.