Import a javascript file from another javascript file



  • The following web page explains how to set up an external javascript file:
    http://doc.qt.nokia.com/latest/qdeclarativejavascript.html
    and import it into a qml file. For example:
    @
    import "factorial.js" as MathFunctions
    @

    Is there any way to include one javascript file into another javascript file in order to use its functions ?



  • bq. Is there any way to include one javascript file into another javascript file in order to use its functions ?

    I spent a lot of time trying to answer that question: I think the answer is "No".

    On the web, there's a lot of discussion about Javascript file "import re-usability as modules" in the context of web pages (e.g., explicit imports for web forms), but apparently that's not how the language itself is intended to be used.

    For example, the QML "import MyFile.js as MyNamespace" is really a QML thing -- greater scalability, but that's through QML, not through Javascript.

    So, I gave up: I have all my coupled Javascript (functions that call each other) in a single file. Further, it forced me to re-organize my design to rely less upon embedding "lots" of logic in Javascript (e.g., do more declarative work within QML and less imperative work in Javascript, and/or push more advanced work to C++).

    In the end, I decided I like these design changes, so I'm happy. However, if you figure out how to get functions in one Javascript file to call those in another, I'd be interested (because I've created a bunch of diagnostic Javascript functions that would be nice to reuse in application-specific Javascript files).



  • I believe this is a limitation of javascript and nothing really to do with QtQuick.

    There are workarounds (check Google).
    An obvious one should be to instead import a QML file that imports a JS file. Then stack these to create as many layers as needed. Though I don't think it is recommended.



  • We had similar situation with QtScript, but there you can use scriptextensions to simulate such functionality. We have a lot of application logic in scripts, so it helped us. But such way is impossible now in qml, because scriptengine used by qml is private member and can't be accessed from your code. And I also don't know other ways to solve it.



  • I faced a similar situation. The only solution I found was to create a separate qml file and import all the JS files there.

    Btw , it will be nice if it will be an import_once feature in QML.



  • Actually, it is possible. All you need is to call

    @
    Qt.include("otherscript.js")
    @

    from your JavaScript file. All functions from the other js file are imported directly into the current namespace.

    I'm certain this used to be in the docs. I've filed http://bugreports.qt.nokia.com/browse/QTBUG-15855 so the docs get fixed.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.