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
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

global JavaScript support inside QML

a new Thread as Follow-Up from

http://supportforums.blackberry.com/t5/Cascades-Development/Doing-all-of-JSON-from-QML/m-p/1906665#M...

 

Peter,

perhaps you can reply your last answer to this Thread ?

 

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: global JavaScript support inside QML

Ekke, the "global object" refers to a sometimes-invisible object which is always in scope, and which provides all the names which are available without having to import or do anything.  For example, you can always call parseInt(), or you can always "throw new Error('foo')" because parseInt and Error are both in the global object.

 

In a browser this is normally called "window", but in an environment like this there may be no direct way to get to the global object to inspect it.  You can, however use this handy routine (as I found out): http://www.nczonline.net/blog/2008/04/20/get-the-javascript-global/

 

With that function, you could assign the return value to, say "g", and do console.log(Object.getOwnPropertyNames(g)) or something like that (notice that both "console" and "Object" are available there because they too are in the global object), and you'd see this list:

 

  • Math
  • NaN
  • undefined
  • Infinity
  • JSON
  • Object
  • Function
  • Array
  • Boolean
  • String
  • Number
  • Date
  • RegExp
  • Error
  • EvalError
  • RangeError
  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError
  • eval
  • parseInt
  • parseFloat
  • isNaN
  • isFinite
  • escape
  • unescape
  • decodeURI
  • decodeURIComponent
  • encodeURI
  • encodeURIComponent
  • print
  • gc
  • Qt
  • XMLHttpRequest
  • DOMException
  • openDatabaseSync
  • SQLException
  • console
  • qsTranslate
  • QT_TRANSLATE_NOOP
  • qsTr
  • QT_TR_NOOP
  • qsTrId
  • QT_TRID_NOOP

Unfortunately it's incomplete, and I don't know why yet.  At the very least, "Application" exists there, providing the ability to do "Application.quit()" from within the Javascript, but I haven't yet found either documentation nor a technique that would let one discover that as with the above.


As for substring() and such, Javascript has several -- substr(), substring(), and slice() -- depending on your goals. These sorts of things should all be standard and in any Javascript/ECMAscript documentation that covers the correct version. (Which version? I'm not 100% clear on that yet...)

 

Note: I posted what's probably in effect a duplicate of this thread as http://supportforums.blackberry.com/t5/Cascades-Development/How-would-you-discover-all-Cascades-QML-... but maybe that one can be left as the narrow question that it is, and this one can be for more general discussion of the specific features in general.


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!
Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: global JavaScript support inside QML

this is really cool Peter, that you found where JavaScript StandardLib functions were hidden.

 

ekke

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: global JavaScript support inside QML

Well, they're global, so not exactly hidden... any Javascript programmer wouldn't even think twice about using those, and would be quite puzzled to find if they *weren't* available globally.

By the way, noting that one of those globals is "Qt", you can find the docs on that one here: http://qt-project.org/doc/qt-4.8/qml-qt.html

I just created https://www.blackberry.com/jira/browse/BBTEN-262 to cover the fact that calling Qt.fontFamilies() gives a segfault.

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!
Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: global JavaScript support inside QML


peter9477 wrote:
Well, they're global, so not exactly hidden... any Javascript programmer wouldn't even think twice about using those, and would be quite puzzled to find if they *weren't* available globally.



I haven't found them before

tried something like

myTextField.text.substring()

and it failed

 

remember: I was a pure native Java developer before - no web, no javascript, ...

 

so: How would I do it to get a substring from a string property using the 'global object' ?

 

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: global JavaScript support inside QML

Yeah, I know your background.  That's why I mentioned that. :-)

It looks like, at least on a Label and TextArea (which I just tested), you can do text.substring(m, n) on it to chop out a piece. Did you see an error in your log output, or what?


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!
Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: global JavaScript support inside QML


peter9477 wrote:

Yeah, I know your background.  That's why I mentioned that. :-)

It looks like, at least on a Label and TextArea (which I just tested), you can do text.substring(m, n) on it to chop out a piece. Did you see an error in your log output, or what?


hmmmmm

 

I know it failed with something like unknown object or so - didn't remember exactly

 

was inside a function

just tried something like

if (name.length > 29) {
   console.debug("JAVASCRIPT ******** "
   +name.substring(29,name.length))
}

 

and it worked

 

magic .... anyway - thanks - now I have an idea what's supported

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

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: global JavaScript support inside QML

I've been exploring the names available in QML on the undocumented Application object.

 

The basic "Application.quit()" works fine, and finding a simple way to do that (really just in a few test apps to make it quicker to exit/restart than swiping up all the time) was what got me going on this.

 

Some of the other names are signals, and those could actually be useful as well:

import bb.cascades 1.0

Page {
    onCreationCompleted: {
        Application.swipeDown.connect(function() {console.log('swipeDown')});
        Application.invisible.connect(function() {console.log('invisible')});
        Application.aboutToQuit.connect(function() {console.log('aboutToQuit')});
        Application.awake.connect(function() {console.log('awake')});
        Application.fullscreen.connect(function() {console.log('fullscreen')});
        Application.thumbnail.connect(function() {console.log('thumbnail')});
    }
}

I played a bit and get the feeling some of these are flaky, which may well be why this isn't documented for QML yet.

 

For someone planning to do as much in QML as possible, the aboutToQuit() signal may be useful. That's called when the app is exiting (whether Application.quit() was called, or the user closed the app from Navigator) and although you can't prevent the termination, you could at least react to it... though I'm not yet sure what  you could usefully do solely in QML... and if you're going to just call C++ you could connect to that signal in C++ in the first place.

 

Maybe the others are more reasonable to work with.  I actually think I had swipeDown() responding at one point, and thought that might allow a pure-QML substitute for the missing Menu class (not yet supported in QML) but at the moment I get nothing for that one... thus my "flaky" comment.


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!
Contributor
kovy
Posts: 26
Registered: ‎08-25-2011
My Device: Z10

Re: global JavaScript support inside QML

You can always use a webview to execute anything you want.  I know it's not clean and it would be better to create function with c++ but can help.

 

Someting like that:

var script = "var path = ''mastring/sfsfsfsdf.sdfsdf";
script += "var lk = 'http://test/' + path.substring(path.lastIndexOf('/'), path.lastIndexOf('.'));";
script += "navigator.cascades.postMessage(lk);"
webView.evaluateJavaScript(script);

Developer
greenmr
Posts: 915
Registered: ‎03-20-2013
My Device: Red LE Developer Z10

Re: global JavaScript support inside QML

I always hang anything I want global access to in any QML file on the global Qt object. You can put any variable or function declaration you want on it and then reference it in any QML ECMAscript code without needing to import or declare anything.


peter9477 wrote:
Well, they're global, so not exactly hidden... any Javascript programmer wouldn't even think twice about using those, and would be quite puzzled to find if they *weren't* available globally.

By the way, noting that one of those globals is "Qt", you can find the docs on that one here: http://qt-project.org/doc/qt-4.8/qml-qt.html

I just created https://www.blackberry.com/jira/browse/BBTEN-262 to cover the fact that calling Qt.fontFamilies() gives a segfault.





Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.