Skip to Content
0

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

Sep 23, 2017 at 10:58 PM

84

avatar image
Former Member

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.

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

4 Answers

Best Answer
avatar image
Former Member Sep 28, 2017 at 12:16 AM
0

I got it worked using UMD format in my library.

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Sep 24, 2017 at 10:40 PM
0

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,


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)
Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Sep 24, 2017 at 04:55 AM
0

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


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

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

0
Joseph BERTHE Sep 25, 2017 at 05:28 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded