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
Posts: 64
Registered: ‎09-10-2009
My Device: Z10, Bold 9700
My Carrier: O2 Germany

Referencing the type of a custom QML component in a JavaScript function

We have a QML file in which we use a custom QML component "BoxContainer" which we have added to the attached objects:

attachedObjects: [
        ComponentDefinition {
            id: boxComponentDefinition
            BoxContainer {
            }
        }
    ]

 

In the same file I have a JavaScript function in which I access the containers used on the page and would like to use "instanceof" to check the type. But the following doesn't work:

if (container instanceof BoxContainer) {
...
}

 I get the error: "ReferenceError: Can't find variable: BoxContainer"

 

Is there a way to reference the type of custom QML components for instanceof checks and type casting?

----

My Twitter: @hendrik_dev
Developer
Posts: 262
Registered: ‎07-13-2012
My Device: PlayBook, DA B, DA C, LE Z10 #102, Q10, Z30
My Carrier: T-Mobile

Re: Referencing the type of a custom QML component in a JavaScript function

You need to give your QML component and "id" then in the JavaScript reference your id...

 

attachedObjects: [
        ComponentDefinition {
            id: boxComponentDefinition
            BoxContainer {
id: myBoxContainer
            }
        }
    ]

...


if (container instanceof myBoxContainer) {
...
}

 

---
Check out my BB10 Cascades Coding site: BBcascades.com & Cascades Blog: bbcascadescode.tumblr.com

My Built for BlackBerry app: The Dive Plan
Developer
Posts: 64
Registered: ‎09-10-2009
My Device: Z10, Bold 9700
My Carrier: O2 Germany

Re: Referencing the type of a custom QML component in a JavaScript function

Thanks for your reply. I tried that, but it didn't work. I get the same error: "ReferenceError: Can't find variable: myBoxContainer"
----

My Twitter: @hendrik_dev
Highlighted
Developer
Posts: 16,992
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Referencing the type of a custom QML component in a JavaScript function

as a workaround you can use the objectName property to store the classname.
http://stackoverflow.com/questions/13923794/how-to-do-a-is-a-typeof-or-instanceof-in-qml

instead of "container instanceof" you use container.objectName==boxContainer
(and set the objectName property in the BoxContainer component to "boxContainer", of course)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 64
Registered: ‎09-10-2009
My Device: Z10, Bold 9700
My Carrier: O2 Germany

Re: Referencing the type of a custom QML component in a JavaScript function


simon_hain wrote:
as a workaround you can use the objectName property to store the classname.

That's what I am doing now in my project - as you probably know since I sat just beside you when we implemented it a moment ago. Smiley Wink

 

But maybe some one knows if there is a way to use "instanceof" in such cases?

----

My Twitter: @hendrik_dev