"Some JS files act more like libraries - they [...] never manipulate QML component instances directly" - middle ground?
-
Quote from Defining JavaScript Resources In QML:
Some JavaScript files act more like libraries - they provide a set of helper functions that take input and compute output, but never manipulate QML component instances directly.
What if I want a JS file that both:
- manipulates QML component instances (that are passed to it as arguments)
- doesn't get a copy of its code and data stored for every QML component instance that imports it?
I could get the "no data stored on every instance" part by, well, not putting global variables in the JS file. But, for some strange reason, a copy of the "code" part seems to be stored for every instance as well. I don't get why this is but I want to know whether I can circumvent it, and at what cost.
-
Hi @Stefan-Monov76, and welcome to the Qt Dev Net!
I'm not very familiar with this part of QML, but I'll see what I can do.
I could get the "no data stored on every instance" part by, well, not putting global variables in the JS file.
Remember to use
.pragma library
too.But, for some strange reason, a copy of the "code" part seems to be stored for every instance as well.
How did you find out that a copy has been stored with every instance?
-
@JKSH said in "Some JS files act more like libraries - they [...] never manipulate QML component instances directly" - middle ground?:
Hi @Stefan-Monov76, and welcome to the Qt Dev Net!
Hi! I'm not that new here though :)
Remember to use
.pragma library
too.Sure, the quote in the title is from the "library-like JS" section (not the "code-behind JS" section) of the doc page, and this does require using
.pragma library
. So I know about that. The problem is that then in this JS resource I "cannot manipulate QML component instances directly", not even if they're passed to me as an argument (that's what the quote stated). Which is a strange limitation.How did you find out that a copy has been stored with every instance?
Well, the doc page I linked to in my original post says this right next:
"As it would be wasteful for each QML component instance to have a unique copy of these libraries [...]"
-
Update: I got a working solution on StackOverflow, you can see it here :)
-
@Stefan-Monov76 said in "Some JS files act more like libraries - they [...] never manipulate QML component instances directly" - middle ground?:
Update: I got a working solution on StackOverflow, you can see it here :)
Thanks for sharing, and for reporting the doc issue :)