Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Import a javascript file from another javascript file
Forum Updated to NodeBB v4.3 + New Features

Import a javascript file from another javascript file

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 6 Posters 18.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    agroyer
    wrote on 3 Dec 2010, 14:02 last edited by
    #1

    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 ?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      charleyb
      wrote on 3 Dec 2010, 14:40 last edited by
      #2

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

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xsacha
        wrote on 3 Dec 2010, 16:46 last edited by
        #3

        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.

        • Sacha
        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on 3 Dec 2010, 16:49 last edited by
          #4

          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.

          1 Reply Last reply
          0
          • 2 Offline
            2 Offline
            2beers
            wrote on 3 Dec 2010, 18:51 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              blam
              wrote on 4 Dec 2010, 03:13 last edited by
              #6

              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.

              1 Reply Last reply
              2

              1/6

              3 Dec 2010, 14:02

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved