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: 22
Registered: ‎03-28-2014
My Device: Blackberry Z10
My Carrier: AT&T

Replace repetitive lines of code in Blackberry QML

I have few lines of code that is common for cases such as onCreationCompleted, onSubmitted and onClicked. Is there a way I put this common code in a function or in a file or as a section within QML and call that function/code whenever needed?

Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: Replace repetitive lines of code in Blackberry QML

common used functions I'm placing in my root

 

per ex

root is TabbedPane wih some Tabs and each tab is a Page or a NavigationPane with a stack of Pages

 

all these Pages placed anywhere on top "know" their root object (their parents)

 

my TabbedPane is always named as

 

id:rootPane

 

then from anywhere you can use

rootPane.myCommonFunction()

 

the only drawback: while coding QML doesn't know about the hierarchy at runtime,

so you get no help from editor

 

and there's an exception of this rule:

if using a Sheet the path is broken. pages on top of a Sheet cannot "see" what's the root

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 22
Registered: ‎03-28-2014
My Device: Blackberry Z10
My Carrier: AT&T

Re: Replace repetitive lines of code in Blackberry QML

Thank you. I though have two questions:

 

  1. Is there any GitHUB code that I can look at to get the example
  2. Is there any alternative approach to QML for Blackberry 10 that is as versatile as say Javascript/Java/PHP where code can be reused more extensively and MVC model can be better implemented
Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Replace repetitive lines of code in Blackberry QML

to your second question: yes there is

 

while it is possible to write all common functions into your root navigation pane, it will spam your root, making it very unreadable, and less reusable if you what to use the same code in different projects

 

what you can do instead of writing all common functions/constants/properties into your root, is to create a custom QtObject, which you add as attachedObject to your root.

 

like this

 

//in your root

import QtQuick 1.0

//...root definition...

attachedObjects: [ Utility { //custom qtobject id: utility }, Strings { //another custom qtobject id: strings } ]

 

//definition of your custom qtobject

 

import bb.cascades 1.2

QtObject {
    
    //store to not call create() all the time
    property variant orange: Color.create("#ffF87A17")

    function getColor(type) {
        return orange; //example for testing
    }
}

 

 

and if you want to call it, just call utility.getColor() / utility.orange from any object under root

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 1,746
Registered: ‎04-08-2010
My Device: Passport Silver, PRIV
My Carrier: Telekom.de, O2, Vodafone

Re: Replace repetitive lines of code in Blackberry QML


pm_jd wrote:

Thank you. I though have two questions:

 

  1. ...
  2. Is there any alternative approach to QML for Blackberry 10 that is as versatile as say Javascript/Java/PHP where code can be reused more extensively and MVC model can be better implemented

to implement MVC from my POV it's the best to use a combination of C++/Qt and QML

search the Cascades Documentation to learn how to exchange data between C++ and QML

 

I never would place business logic in QML

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development native Mobile Business Apps
BlackBerry 10 | Qt Mobile (Android, iOS)
Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org
mobile-development: http://appbus.org Twitter: @ekkescorner
Developer
Posts: 22
Registered: ‎03-28-2014
My Device: Blackberry Z10
My Carrier: AT&T

Re: Replace repetitive lines of code in Blackberry QML

Thank you all! This helps.