cancel
Showing results for 
Search instead for 
Did you mean: 

How to call a JS function in a controller (this function is defined in another folder) ?

Former Member
0 Kudos

Hi ui5 experts,

I am new to sap ui5 and working on sap web IDE demo version.

This is bellow an application I am working on. Basically what I want to do is very simple. Let's say we have a custom js function called hello() defined in "myApp_toto/tools/date.js" file and I want to call this hello function in the controller Address.controller.js, this controller triggers my function hello() when a button called "sayHello" is clicked on. The button sayHello is defined in the Address.js.view

I've tried many things with dependencies sap.ui.define(...), Query.sap.registerModulePath(), sap.ui.require(), but still not working.

If you faced the same issue or have any good solution, please share !

Thank you.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

I got it worked using UMD format in my library.

Answers (3)

Answers (3)

Joseph_BERTHE
Active Contributor
0 Kudos

Hello,

First of all, PLEASE do not develop with JS view, use XML. This is the best practice recommanded by SAP !

Secondly, did you register your library into the manifest.json?

Regards

Joseph

Former Member
0 Kudos

Hi Rahul,

Thank you for your reply.

Here is my view where the button that triggers hello() function is defined.

Here is my controller where the dependency named moment is injected:

Here is my module moment.js where dispResult is defined:

In debugging this what I am getting when button is clicked on: ==> my dipsResult is not recognized:

moment.js file is loaded but its content is still not accessible from my controller.

any other idea is welcom.

thanks,

Former Member
0 Kudos

Hi,

Below is one such example from Fiori Master Detail App template:

Here models.js is defined inside model folder and this js file contains one function createDeviceModel, which is returned in a JavaScript object.

Now in component.js file the models.js file is loaded with asynchronous module defination and called as below:

Here sap.ui.demo.masterdetail is the namespace of app (root).

Regards,

Rahul

Former Member
0 Kudos

I already tried somthing like this. Please see my answer for more details !