Skip to Content
0

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

Nov 26, 2017 at 11:29 AM

111

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Irfan Gokak Nov 26, 2017 at 01:06 PM
0

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.

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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

0

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

0
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.

0
Joseph BERTHE Nov 26, 2017 at 09:16 PM
0

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

Show 7 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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

0

Look at this Link

0
Former Member
Joseph BERTHE

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]


0
Former Member
Former Member

Any help would be appreciated...

0

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)
0

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

0
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
0
Arjun Biswas Dec 06, 2017 at 04:21 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded