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
sketch34
Posts: 54
Registered: ‎05-23-2012
My Device: Developer

Organising QML

I have this same block of QML (inside the onTriggered) in several places:

 

List View {
onTriggered: {
// START BLOCK var chosenItem = <Some DataModel>.data(indexPath); var contentpage = detailsPageDefinition.createObject(); contentpage.titleName = chosenItem.ContentName ... Set a whole bunch of other property aliases, always the same. ... contentpage.initDetailsPage(chosenItem.ContentType, chosenItem.ContentId); // Push the content page to the navigation stack. <Some NavigationPane>.push(contentpage);
// END BLOCK } }

Obviously it's poor style to have this big chunk copied and pasted a bunch of times throghout my QML. How can I have this in a separate file and included where I need it? The only things that change are the parts in < >.

 

Thanks!

----
I know exactly where the problem *might* be.
Developer
d3v3l0p3r
Posts: 168
Registered: ‎07-19-2012
My Device: Q5, Q10, Z10, Z30, PlayBook

Re: Organising QML

[ Edited ]

There are probably some JavaScript external function approaches you can do (I am not familiar with them) but one way I have done it is created a function in my root element (TabbedPane), gave it id: root and then anywhere in QML where QML is in the tree of that root I just call root.myFunction to call this function. Hope this helps. 

 

PS: the only problem with this approach is that if the common function uses local elements / properties which may not be directly referencable from the root you need to pass them in the function parameters. This is probably best approach for more static, calculation type functions.

---
If you find my post helpful please "like" it and "accept as a solution"
Developer
Zmey
Posts: 1,514
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Organising QML

Hi,

 

This document describes how to import external Javascript files in QML. I think this should work with Cascades too:

http://doc.qt.digia.com/stable/qdeclarativejavascript.html

 


Andrey Fidrya, @zmeyc on twitter
Developer
igosoft
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon

Re: Organising QML