(Difference between revisions)

Creating APIBridge JavaScript Binding Code

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
m (Protected "Creating APIBridge JavaScript Binding Code" ([edit=sysop] (indefinite) [move=sysop] (indefinite)))
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot addition of Template:ArticleMetaData)
Line 1: Line 1:
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
|id= <!-- Article Id (Knowledge base articles only) -->
|language=<!-- Language category code for non-English topics - e.g. Lang-Chinese -->
|review-by=<!-- After re-review: [[User:username]] -->
|review-timestamp=<!-- After re-review: YYYYMMDD -->
|update-by=<!-- After significant update: [[User:username]]-->
|update-timestamp=<!-- After significant update: YYYYMMDD -->

Revision as of 01:26, 4 November 2011

Article Metadata
Created: User:Kbwiki (11 Feb 2010)
Last edited: hamishwillee (04 Nov 2011)


Binding JavaScript™ Code

The binding code for the EchoServlet example can be found in \bindings\js\sample.js. This file uses the APIBridge class, which is contained in the apibridge.js file that ships with APIBridge.

The code for this example is:

  var Sample = {
echo: function( msg, err, onSuccess, onError) {
"/sample/echo?msg="+encodeURIComponent(msg) +"&err="+encodeURIComponent(err),
if (req.responseText)
onSuccess (req.responseText);
onError );

Essentially, the binding code translates the parameters passed into a URI-encoded GET string by using the APIBridge.Internal._sendRequest function. It then analyses the response from the framework and calls the appropriate callback.

If you dissect the URL portion, you will see two parts:

  • Request: /sample/echo. This request will get routed to your plug-in as specified in the ECOM Plug-in resource file.
  • Parameters: ?msg="+encodeURIComponent(msg)+"&err="+encodeURIComponent(err). The parameters are extracted at the servlet’s ServiceL function by using the RQueryParser class and this function:


How it is used

The code below shows how a Web Runtime (WRT) application might use this newly created function:

'''function'''(text) //OnSuccess callback
<nowiki> {</nowiki>
output.innerHTML <nowiki>+</nowiki>= "The APIBridge returned: "<nowiki>+</nowiki>text<nowiki>+</nowiki>"<nowiki><</nowiki>br/<nowiki>></nowiki>";
'''function''' (err) //OnError callback
output.innerHTML <nowiki>+</nowiki>= "Error: "<nowiki>+</nowiki> err.status <nowiki>+</nowiki> ". Message: "<nowiki>+</nowiki>err.responseText<nowiki>+</nowiki>"<nowiki><</nowiki>br/<nowiki>></nowiki>";

JavaScript Binding API

The binding API for JavaScript™ can be found in the apibridge.js file that ships with APIBridge.

SendRequest function

APIBridge.Internal._sendRequest: function( path, parameters, onSuccess, onError )

This function is used to send a raw request to the APIBridge server; it takes care of security and access control for the developer.

Parameter Description
path URI path to the plug-in including any GET parameters. The http://localhost part should NOT be included here.
parameters String that will be passed in the body of the HTTP request. If this parameter is not null, the request will be a POST request, otherwise it will be a GET.
onSuccess Callback function that returns the XMLHttpRequest object as the single parameter. It is called when the server reports a 200 HTTP status code.
onError Callback function that returns the XMLHttpRequest object as the single parameter. It is called when the server reports an HTTP status code other than 200, the access is not granted, or the path is not associated with any plug-in.
44 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.