10-23-2012 05:33 AM
Hi,
I am loading a HTML file in WebView. I need to pass data from qml to JavaScript of the HTML file. Is there any way to create an interface between JavaScript code & qml to exchange data among them?
Thanks,
Nishant Shah
Solved! Go to Solution.
10-23-2012 06:53 AM
Hi,
I did manage to find the solution. Just go through this link & you are done:
https://developer.blackberry.com/cascades/referenc
Sorry to bother you all !
Regards,
Nishant Shah
10-23-2012 08:41 AM
Quoting from that page (to make the answer here more specific for those who may read it in future): "You can interact with JavaScript executing on the page by using postMessage() and connecting to the messageReceived() signal. JavaScript can communicate back by using the "navigator.cascades.postMessage()" function and storing a message handler in the "navigator.cascades.onmessage" property."
Note: You may discover as I did that these two interfaces are a bit limited in what you can send through them. They even appear to have different limitations. For example, in one of the directions, you can't send anything that contains embedded double-quotation marks, as I recall!
What you can do, if you don't want to be limited to simple strings or similarly primitive data, is encode the data before sending, and decode it upon receiving. There are routines that work on both sides for doing this.
In the WebView, you can use the global (i.e. on the "window" object) btoa() and atob() routines to encode to base64 before sending, or decode received messages that were base64-encoded in the QML side.
In QML, where we don't have the global window object, we do have the global Qt object, which has atob() and btoa() routines to match.
If you'd like to send "rich" data, you can also use JSON.stringify() on either side, to serialize more complex JavaScript objects (or arrays) to strings prior to the atob() encoding, then on the receiving side use btoa() followed by JSON.parse() to get the strings turned back into JavaScript objects.
10-25-2012 07:09 AM
Hi peter,
Thnaks buddy that was a pretty good description. and yeah, I found it a bit limited indeed.
Is there any api to create custom signals like messageReceived() signal for WebView? I need to insert data, update data etc in db when clicked on certain buttons in HTML. I want to create different interface for them.
And also if I want to return some data from QML to javascript at times like:
var boolVar = navigator.cascade.createTable();
I guess single navigator.cascades.onmessage handler would make quite complicated.
Regards,
Nishant Shah
10-25-2012 09:34 AM
10-25-2012 10:01 AM
Hi peter,
Right now I did the way you suggested only. Still It would be better if we had different handler & could have been simpler to create cross applications. Anyways thanks a ton for replying
Regards,
Nishant Shah