Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Posts: 237
Registered: ‎05-21-2012
My Device: BlackBerry RED Z10
My Carrier: WiFi

Call a JS function from another QML

I have 2 QMLs, the 1st one has the JS Function, I want to call the JS Function in the 2nd QML.

I believe this can be done but can't find any resources how to do this. Any suggestions please?

Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: Call a JS function from another QML

[ Edited ]

if your js function is not in main .qml this method should help out (if it is, move it to 2nd qml)

//reasoning for moving is the only way i have found to access something from main qml in another is to use qsettings, but that doesn't quite work out for functions.


1st qml (with function)

Container {
   id: root
   property alias thisRoot: root

   function myFunction(){
       //do stuff

main.qml (or 2nd qml)

Button {
  onClicked: {
Posts: 426
Registered: ‎04-10-2011
My Device: Z30

Re: Call a JS function from another QML

It depends on the relationship of the QML files as well as what the function achieves.
If the function is just some computation function that is independent of the QML component it is declared in, you can extract it to a .js file.
If the 2nd QML is included in the 1st on (so a custom component display), then you can just call the function similar to what slashkyle proposed.
Check out my apps in the BlackBerry World
Visit my developer blog alopix.net
BBM Channel: C0047B612