1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
28 \page qtqml-javascript-resources.html
29 \title Defining JavaScript Resources In QML
30 \brief Description of how JavaScript files may be defined for use in QML
32 The program logic for a QML application may be defined in JavaScript. The
33 JavaScript code may either be defined in-line in QML documents, or separated
34 into JavaScript files (known as \c {JavaScript Resources} in QML).
36 There are two different kinds of JavaScript resources which are supported in
37 QML: code-behind implementation files, and shared (library) files. Both kinds
38 of JavaScript resource may be \l{qtqml-javascript-imports.html}{imported} by
39 other JavaScript resources, or included in \l{qtqml-modules-topic.html}
42 \section1 Code-Behind Implementation Resource
44 Most JavaScript files imported into a QML document are stateful implementations
45 for the QML document importing them. In these cases, each instance of the QML
46 object type defined in the document requires a separate copy of the JavaScript
47 objects and state in order to behave correctly.
49 The default behavior when importing JavaScript files is to provide a unique,
50 isolated copy for each QML component instance. If that JavaScript file does
51 not import any resources or modules with a \c{.import} statement, its code will
52 run in the same scope as the QML component instance and consequently can access
53 and manipulate the objects and properties declared in that QML component.
54 Otherwise, it will have its own unique scope, and objects and properties of the
55 QML component should be passed to the functions of the JavaScript file as
56 parameters if they are required.
58 An example of a code-behind implementation resource follows:
63 import "my_button_impl.js" as Logic // a new instance of this JavaScript resource is loaded for each instance of Button.qml
74 onClicked: Logic.onClicked(rect)
81 var clickCount = 0; // this state is separate for each instance of MyButton
82 function onClicked(btn) {
84 if ((clickCount % 5) == 0) {
85 obj.color = Qt.rgba(1,0,0,1);
87 obj.color = Qt.rgba(0,1,0,1);
92 In general, simple logic should be defined in-line in the QML file, but more
93 complex logic should be separated into code-behind implementation resources
94 for maintainability and readability.
96 \section1 Shared JavaScript Resources (Libraries)
98 Some JavaScript files act more like libraries - they provide a set of helper
99 functions that take input and compute output, but never manipulate QML
100 component instances directly.
102 As it would be wasteful for each QML component instance to have a unique copy of
103 these libraries, the JavaScript programmer can indicate a particular file is a
104 shared library through the use of a pragma, as shown in the following example.
110 var factorialCount = 0;
112 function factorial(a) {
115 // factorial recursion
117 return a * factorial(a - 1);
122 // recursion base-case.
126 function factorialCallCount() {
127 return factorialCount;
131 The pragma declaration must appear before any JavaScript code excluding comments.
133 Note that multiple QML documents can import \c{"factorial.js"} and call the
134 factorial and factorialCallCount functions that it provides. The state of the
135 JavaScript import is shared across the QML documents which import it, and thus
136 the return value of the factorialCallCount function may be non-zero when called
137 within a QML document which never calls the factorial function.
144 import "factorial.js" as FactorialCalculator // this JavaScript resource is only ever loaded once by the engine, even if multiple instances of Calculator.qml are created
149 property int input: 17
150 text: "The factorial of " + input + " is: " + FactorialCalculator.factorial(input)
154 As they are shared, .pragma library files cannot access QML component instance
155 objects or properties directly, although QML values can be passed as function