Skip to Content
avatar image
Former Member

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

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

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

    I got it worked using UMD format in my library.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    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.

    thanks,

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 24, 2017 at 04:55 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 25, 2017 at 05:28 AM

    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

    Add comment
    10|10000 characters needed characters exceeded