Skip to Content

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

Sep 23, 2017 at 10:58 PM


avatar image

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(...),, sap.ui.require(), but still not working.

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

Thank you.

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

4 Answers

Best Answer
Rachid Ui5 Sep 28, 2017 at 12:16 AM

I got it worked using UMD format in my library.

10 |10000 characters needed characters left characters exceeded
Rachid Ui5 Sep 24, 2017 at 10:40 PM

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.


image1.png (14.5 kB)
image2.png (12.7 kB)
image3.png (32.3 kB)
image4.png (27.2 kB)
image5.png (11.1 kB)
10 |10000 characters needed characters left characters exceeded
Rahul Gupta Sep 24, 2017 at 04:55 AM


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



pic2.jpg (100.0 kB)
pic1.jpg (40.1 kB)
Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

Joseph BERTHE Sep 25, 2017 at 05:28 AM


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?



10 |10000 characters needed characters left characters exceeded