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
Contributor
Posts: 29
Registered: ‎12-20-2011
My Device: Blackberry 9900
My Carrier: O2

QML and Javascript files (and Objects / prototypes)

Hi all,

 

I'm trying to get a javascript library working via an import from QML. So this is a native application on bb 10.1, and I'm using QML to import some javascript functionality.  I have the following import:

 

import "rsa.js" as RSA

 rsa.js has some basic javascript object code, which looks like:

 

// "empty" RSA key constructor
var RSAKey = function () {
  this.e = 0;
};

RSAKey.prototype._test = function () {
	return this.e;
};

function _new ()
{
	return new RSAKey();
}

function _entry ( _rsa )
{
	return _rsa._test();
}

 

But I can't seem to use this functionality to initialise and/or control an RSAKey object. So if I do:

 

var rsa = RSA._new();
RSA._entry( rsa );

 

It freezes and cannot evaluate. This seems to be related to the namespacing and how bb QML imports or manages the js imported. Can anyone shed some light on why this doesn't work and what I might do to operate on objects & the prototype parameter within imported javascript?

 

 

 

 

 

Retired
Posts: 81
Registered: ‎09-09-2013
My Device: BlackBerry Z10
My Carrier: Etisalat UAE

Re: QML and Javascript files (and Objects / prototypes)

Hello,

 

Your code working with me, calling it on button prints 0 or whatever value assigned to this.e

 

Where is the JS file is located? Is it located in the asset folder? not sure about the exact problem...Is it where/when to initialise the JS object?

 

import "rsa.js" as RSA

 

in QML file:

Button { text: "Test JS" onClicked: { var rsa = Test._new(); console.log(Test._entry(rsa)); //prints zero or any value assigned } }

 

Thanks,

Kareem.

 

Kareem ELSayed | Application Development Consultant | Developer Relations | BlackBerry | Twitter:@kemobyte
Contributor
Posts: 29
Registered: ‎12-20-2011
My Device: Blackberry 9900
My Carrier: O2

Re: QML and Javascript files (and Objects / prototypes)

Hey,

 

Thanks for spending the time running the code.

 

I'm running the code within a javascript file. So there are two files interacting with each other:

 

card.js

rsa.js

 

each imported in a similar way:

 

import "rsa.js" as RSA
import "card.js" as Cards

 

Maybe it's to do with how they're imported / isolated from each other?  Running on a Z10, version 10.0.9 (can't update, don't go there).

 

James

 

Retired
Posts: 81
Registered: ‎09-09-2013
My Device: BlackBerry Z10
My Carrier: Etisalat UAE

Re: QML and Javascript files (and Objects / prototypes)

[ Edited ]

Hi James,

I really can see no reason for code to not work fine, since you imported both files to the memory, You can access RSA functions within Cards.JS file. I've tested it by creating file and imported it as X includes only one function "add" which increment the this.e by 1 then called this function using X.add from the RSA.JS file and it works fine.

I will be able to help if you posted the entire code.

Thanks,
Kareem.

Kareem ELSayed | Application Development Consultant | Developer Relations | BlackBerry | Twitter:@kemobyte
Retired
Posts: 418
Registered: ‎07-18-2012
My Device: Q10
My Carrier: Bell Canada

Re: QML and Javascript files (and Objects / prototypes)

For performance reasons - I'd recommend using the Ceritcom or OpenSSL libraries for RSA math...

--
Rob is no longer associated with BlackBerry.
Contributor
Posts: 29
Registered: ‎12-20-2011
My Device: Blackberry 9900
My Carrier: O2

Re: QML and Javascript files (and Objects / prototypes)

OK, i tried to reproduce this within an isolated environment but you're right it does seem to work. I was also surprised to see I was able to call directly into objects within 'rsa' without the need for the 'RSA' import as key. Something seriously wierd is going on in my code then..

 

For context I've ported this:

 

http://www-cs-students.stanford.edu/~tjw/jsbn/

 

into the QML js layer (don't ask..). It required reasonably significant changes to work, some of those changes related to this problem.

 

I'll need to do some more investigation and get back with some more detailed / specific info, thanks for the help anyway. 

 

James