Beru

How to Install

The Basic Ebook Reader for Ubuntu

Javascript Libraries

16 August 2013


Up to this point, I’ve been calling everything that goes into a .qml file “QML”. I guess that’s not actually correct—QML is just the declarative code. The procedural code for event handlers is actually Javascript.

And since it’s Javascript, you can import existing libraries into your QML (or maybe it’s Javascript) code. The QML engine will execute it just as any browser engine would.

Well, almost.

For some reason, the QML engine puts restrictions on modifications to the global object. For those of us who aren’t Javascript wizards, this doesn’t seem like a big deal. But people who write Javascript libraries often use the global object to work around Javascript’s lack of (built-in) namespaces. These tricks will keep libraries from working with QML. Since QML actually does sensible namespacing of Javascript, there’s no need for these things, so you can just take them out.

How much of a hassle that is will depend on the details of your library. In my case, I’m integrating the JSZip library, and the modifications were pretty minimal. Instead of attaching objects to the root object to pass them from one script file to another, I just used Qt.include() to include one script file in the other.

May all your integrations go so smoothly.

comments powered by Disqus