[Qt-qml] Removal of the Script element
aaron.kennedy at nokia.com
aaron.kennedy at nokia.com
Mon Mar 29 02:09:28 CEST 2010
Hi Mathias,
On 27/03/10 4:20 AM, "Malmqvist Mathias (Nokia-SU/London)"
<mathias.malmqvist at nokia.com> wrote:
> Hi guys,
>
> Has the removal of the Script element in Qt 4.7 also removed the
> possibility to scope the imported scrips?
>
> Previously, adding importing a script file like this
> Item {
> id: myRoot
> Script { source: "foo.js" }
> [...]
> }
> made everything defined in foo.js available to all child elements of myRoot,
> event component ones defined in other qml files.
>
> Changing this to the new syntax:
> import "foo.js" as Foo
> Item {
> id: myRoot
> [...]
> }
>
> makes the definitions in foo.js available as e.g. Foo.bar(), but only to
> elements defined in the same file as myRoot.
>
> Now of course one could add
> import "foo.js" as Foo
> to all files that need access to its definitions. The problem is it appears
> anything in foo.js will be re-instantiated every time it's imported (actually
> it seems every time a component defined in the file that imports the js-file
> is instantiated).
>
> Am I missing something? Is thee a way to still import the definitions in a
> script
> file as singletons available to the whole QML application?
It depends :) JS files were never imported as singletons - but they were
inherited by sub-components. In some cases this is indistinguishable from a
true singleton, but in some cases it is not.
If you want a *true* singleton (a single copy for the entire application)
you're in luck. If the JS file starts with ".pragma library" QML will
import it as a singleton. This is documented here:
http://doc.trolltech.com/4.7-snapshot/qdeclarativejavascript.html
We plan in reintroducing something functionally equivalent to the old
mechanism at some point, but probably not for the 4.7 release. You can
still fake it with proxy functions on your root object - ugly but workable.
Cheers,
Aaron
More information about the Qt-qml
mailing list