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: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Static class instance.

[ Edited ]

It should work on other pages as well (in the same context). In some situations it'll become inaccessible, for example if there is a ListItemComponent in hierarchy - they have a different context.

Also, I recall that showing a Sheet from TabbedPane can lead to issues with accessing outer variables from the sheet (this is a bug).

Variables still can be accessed in these situations using workarounds different for each situation. Basically, declare a JavaScript function in a parent where the singleton is still accessible. This function should return a singleton object. Call this function using 'parent' property of top level item in child: someField: topItemId.parent.getSingleton()

For ListItemComponent use topItemOfListItemComponent.ListItem.view.yourJavaScriptfunction(). The function should be declared in ListView, it will have the access to outer context and thus can return a singleton.

If you have many pages and traversing the parents is undesirable, probably another workaround can be created. For example, creating and registering an instantiable C++ class which can return a singleton object, and declaring that class on every page where the access to singleton is needed as attached object. I haven't tried this approach, but I think it should work.


Andrey Fidrya, @zmeyc on twitter
Highlighted
Developer
Posts: 16,986
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Static class instance.


AnugrahThomas wrote:

setProperty method is working fine when we are pushing a qml page from a cpp file. But how do we set the singleton instance property to a qml page if its being pushed from another qml page.


The qml pages that have a common root share its context, so if you set it on your main.qml (probably the navigationPane) all pages created from there will have access to it, too (and all other properties, functions etc).

----------------------------------------------------------
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