2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3/*!
4\page qtqml-javascript-imports.html
5\meta {keywords} {qmltopic}
6\title Importing JavaScript Resources in QML
7\brief Description of how to import and use JavaScript resources in QML documents
8
9\l{qtqml-javascript-resources.html}{JavaScript resources} may be imported by
10QML documents and other JavaScript resources. JavaScript resources may be
11imported via either relative or absolute URLs. In the case of a relative URL,
12the location is resolved relative to the location of the \l {QML Documents}{QML document} or
13\l{qtqml-javascript-resources.html}{JavaScript Resource} that contains the
14import. If the script file is not accessible, an error will occur. If the
15JavaScript needs to be fetched from a network resource, the component's
16\l {QQmlComponent::status()}{status} is set to "Loading" until the script has
17been downloaded.
18
19JavaScript resources may also import QML modules and other JavaScript
20resources. The syntax of an import statement within a JavaScript resource
21differs slightly from an import statement within a QML document, which is
22documented thoroughly below.
23
24\section1 Importing a JavaScript Resource from a QML Document
25
26A QML document may import a JavaScript resource with the following syntax:
27\code
28import "ResourceURL" as Qualifier
29\endcode
30For example:
31\code
32import "jsfile.js" as Logic
33\endcode
34
35Imported JavaScript resources are always qualified using the "as" keyword. The
36qualifier for JavaScript resources must start with an uppercase letter, and must
37be unique, so there is always a one-to-one mapping between qualifiers and JavaScript
38files. (This also means qualifiers cannot be named the same as built-in JavaScript
39objects such as \c Date and \c Math).
40
41The functions defined in an imported JavaScript file are available to objects
42defined in the importing QML document, via the
43\c{"Qualifier.functionName(params)"} syntax. Functions in JavaScript resources
44may take parameters whose types can be any QML value types or
45object types, as well as normal JavaScript types. The normal
46\l{qtqml-cppintegration-data.html}{data type conversion rules} will apply to
47parameters and return values when calling such functions from QML.
48
49\section1 Imports Within JavaScript Resources
50
51In \c {QtQuick 2.0}, support has been added to allow JavaScript resources to import
52other JavaScript resources and also QML type namespaces using a variation of
53the standard QML import syntax (where all of the previously described rules and
54qualifications apply).
55
56Due to the ability of a JavaScript resource to import another script or QML
57module in this fashion in \c {QtQuick 2.0}, some extra semantics are defined:
58\list
59\li a script with import statements will not inherit the imports from the QML document that imported it (accessing Component.errorString will fail, for example)
60\li a script without import statements will inherit the imports from the QML document that imported it (accessing Component.errorString will succeed, for example)
61\li a shared script (defined as .pragma library) will not inherit the imports from QML documents even if the script does not import other scripts or modules
62\endlist
63
64The first semantic is conceptually correct, given that a particular script
65might be imported by any number of QML files. The second semantic is retained
66for the purposes of backwards-compatibility. The third semantic remains
67unchanged from the current semantics for shared scripts, but is clarified here
68in respect to the newly possible case (where the script imports other scripts
69or modules).
70
71\section2 Importing a JavaScript Resource from Another JavaScript Resource
72
73A JavaScript resource may import another in the following fashion:
74\code
75import * as MathFunctions from "factorial.mjs";
76\endcode
77Or:
78\code
79.import "filename.js" as Qualifier
80\endcode
81
82The former is standard ECMAScript syntax for importing ECMAScript modules, and
83only works from within ECMAScript modules as denoted by the \c mjs file
84extension. The latter is an extension to JavaScript provided by the \c QML
85engine and will work also with non-modules. As an extension superseded by the
86ECMAScript standard, its usage is discouraged.
87
88When a JavaScript file is imported this way, it is imported with a qualifier.
89The functions in that file are then accessible from the importing script via the
90qualifier (that is, as \tt{Qualifier.functionName(params)}).
91
92Sometimes it is desirable to have the functions made available in the importing
93context without needing to qualify them. In this case ECMAScript modules and the
94JavaScript \c import statement should be used without the \c as qualifier.
95
96For example, the QML code below left calls \c showCalculations() in \c script.mjs,
97which in turn can call \c factorial() in \c factorial.mjs, as it has included