cancel
Showing results for 
Search instead for 
Did you mean: 

SAP UI5 - How to call controller function from outside of UI5 framework

former_member602416
Participant

I am new in UI development and looking for a help on below issue. I have created a SAP UI5 project which has one button defined in view file and function defined in controller file which gets called on press event of button.

I have a requirement to click button B1 from outside of UI5 framework and also invoke controller function defined on press event of button.

Can someone please suggest how this can be done.

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

maheshpalavalli
Active Contributor

Hi Swati Garg,

You can directly access the button and call firePress() method right?

But you cannot directly access the button outside with the id you have given inside the view.. UI5 framework will generate a different ID based on the component name, view name and the button id. using that id you need to get the button instance.

If the button id is "ButtonCustID" then the UI5 generated will be "__ComponentName---ViewName--ButtonCustID".

sap.ui.getCore().byId("").firePress("__ComponentName---ViewName--ButtonCustID");

BR,

Mahesh

jorge_cabanas
Participant
0 Kudos

Hi Mahesh,

My first though was to solve this as you correctly said. But after that , I read again the issue and noticed that:
"I have a requirement to click button B1 from outside of UI5 framework".

It could mean that in the right moment he needs to fire the click event, the controller or even the button could be not loaded yet.
That make a little sense if we see that he wants to "invoke the controller".


So I think in this case the global generated ID by SAPUI5 as you suggested couldn't be the solution.

Do you agree?

maheshpalavalli
Active Contributor

Yeah I agree, but you can check the old question posted by OP where the OP wants to replicate the actions that there performed by the user which were saved somewhere.. So I assumed that OP has already opened the app and performing some admin action to run through the actions performed by the user..

So many assumptions I made here 😄 maybe Swati Garg can tell us the actual scenario.

former_member602416
Participant
0 Kudos

Hi Mahesh,

Many thanks for your inputs. I have mEventRegistry attached to element. Will that be helpful to call the press event? I can see view and controller id as well at mEventRegistry.press.Scopes.Closure node. But as you mentioned cannot find UI5 generated Id anywhere.

maheshpalavalli
Active Contributor

It's just the button id.. did you try to use the code that I mentioned above?

Answers (2)

Answers (2)

junwu
Active Contributor
0 Kudos
document.getElementById('yourbuttonid').click()
former_member602416
Participant
0 Kudos

Thanks for your response but what you mentioned is another way of clicking a button. I have resolved this issue by calling press event as mentioned by Mahesh.

Thanks

junwu
Active Contributor
0 Kudos

it didn't trigger ui5's event handler?

junwu
Active Contributor
0 Kudos

what's your scenario?

former_member602416
Participant
0 Kudos

I need to capture user actions performed on sap ui5 web page and playback same after sometime. So if user entered a text and clicked on button then I want to playback same from outside. I am able to capture and playback input field but click of button doesn't call function defined on press event in controller file during playback. So i need to know how to invoke controller function from outside. Below code clicks the button.

var targBtn = document.querySelector ("#" + target.id); //target.id is id of control element.

var clickEvent = document.createEvent ('MouseEvents');

clickEvent.initEvent ('click', true, true);

targBtn.dispatchEvent (clickEvent);

My code to capture and playback is written in .Js file and I have injected this file in index file of sapUI5 app.