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
aklowther
Posts: 47
Registered: ‎01-15-2013
My Device: Z10
My Carrier: N/A

Looking for QML/Javascript functions to only run on call

I am looking for a way to prevent certain functions from being run when a QML page is loaded.  For example, a checkbox : onCheckChanged{} signal.  When my page is loaded, it runs the code I have in the onCheckChanged section.

 

I am only having these issues with Javascript/QML functions, not C++.  Any suggestions to prevent this?

 

Thanks

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Looking for QML/Javascript functions to only run on call

I use a simple technique to good effect.

 

In either the top-level component of the page in question, or the individual item in question, add a boolean property to flag whether you're initialized yet. I try to be creative so I call it "initialized". Set it true by default.

 

In the onCreationCompleted, do stuff then set it false at the end.

 

Then in the handler itself, just return immediately if it's not initialized.

 

Pseudo-example:

Page {
    property bool initialized: false

    CheckBox {
        onCheckedChanged: {
            if (!initialized) return;

            // do regular stuff here
        }
    }

    onCreationCompleted: {
        // do initialization stuff here...

        initialized = true;
    }
}

 


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Developer
aklowther
Posts: 47
Registered: ‎01-15-2013
My Device: Z10
My Carrier: N/A

Re: Looking for QML/Javascript functions to only run on call

Thanks, I've been using that technique for other functions on other pages but figured there had to be a better way.
Please use plain text.