Welcome!

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

Reply
Developer
nemory
Posts: 217
Registered: ‎05-21-2012
My Device: BlackBerry RED Z10

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?

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

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: {
     thisRoot.myFunction();
  }
Developer
alopix
Posts: 416
Registered: ‎04-10-2011
My Device: Z10 LE & PlayBook

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 blogs /dev/alopix and /home/alopix
BBM Channel: C0047B612