JavaScript extension example

The JavaScript Framework Reference describes all of the extensions and their methods available for the QNX CAR platform. In this section, let's take the qnx.volume extension as an example. As you can see in the figure below, the documentation for qnx.volume shows that this extension has two methods associated with it: get and set.

Figure 1. Documentation for the qnx.volume extension.

The documentation says that the methods in the volume extension must be called as follows:

qnx.volume.methodName()

To get the volume, you call qnx.volume.get(). To set the volume, you call:

qnx.volume.set(x)

where x = [0, 100].

Calling extension methods

To call an extension method, you need to ensure that the extension is included in the config.xml file for your app. For the qnx.volume example, we would add the following line to the config.xml file:

<feature id="qnx.volume" required="true" version="1.0.0.0"/>

To call the get method in the qnx.volume extension, use qnx.volume.get() from your app. That's all there is to do. Do the same thing to call any method in any extension.

Listening for events

Most extensions also have events that can be sent to your app asynchronously. To find out which events are available for a particular extension, open the index.js file and look for the events defined inside the _actionMap variable.

Note: If there's no _actionMap variable, then the extension has no events.

In qnx.volume, there's a single event called volumeupdate. To register a callback for this event, add the following line to your app:

blackberry.event.addEventListener('volumeupdate', myFunction);

From this point forward, whenever the volume on the system changes, the myFunction function will be called with arguments containing the volume information. You can also remove the event listener at any time by calling:

blackberry.event.removeEventListener('volumeupdate', myFunction);

The webworksready event

One of the few very important steps to follow when using WebWorks extensions is to make sure that all of the extensions are loaded and ready to use before calling them for the first time.

To help with this, WebWorks has a webworksready event that you can listen for. To listen for this event, add the following code after the webworks.js script tag in the head of your page:

window.addEventListener("load", function(e) {
     document.addEventListener("webworksready", onReady);
}, false);

Next, create a function called onReady. This function will be called when WebWorks and all of its extensions are loaded and ready for use. Inside this function is where you can add event listeners for framework events, and start calling extension methods.