×
Namespaces

Variants
Actions
(Difference between revisions)

Creating APIBridge JavaScript Binding Code

From Nokia Developer Wiki
Jump to: navigation, search
 
hamishwillee (Talk | contribs)
m (Hamishwillee - Subedit)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category:Symbian Web Runtime]][[Category:JavaScript]]
 +
{{Abstract|This article shows how to create APIBridge Binding Code in JavaScript, making it possible to access device services in Symbian WebRuntime widgets.}}
 +
{{ArticleMetaData
 +
|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 -->
 +
|creationdate=20100211
 +
|author=[[User:Kbwiki]]
 +
}}
 +
{{SeeAlso|[http://www.developer.nokia.com/Resources/Tools_and_downloads/Other/APIBridge/ APIBridge Reference]}}
 
__TOC__
 
__TOC__
  
  
==Binding JavaScript™ Code<br>==
+
==Binding JavaScript™ Code ==
 
The binding code for the EchoServlet example can be found in '''\bindings\js\sample.js'''. This file uses the <tt>APIBridge</tt> class, which is contained in the '''apibridge.js''' file that ships with APIBridge.
 
The binding code for the EchoServlet example can be found in '''\bindings\js\sample.js'''. This file uses the <tt>APIBridge</tt> class, which is contained in the '''apibridge.js''' file that ships with APIBridge.
  
 
The code for this example is:
 
The code for this example is:
  
<code>
+
<code javascript>
 
+
var Sample = {
  var Sample = {
+
  echo: function( msg, err, onSuccess, onError) {
  echo: function( msg, err, onSuccess, onError) {
+
  APIBridge.Internal._sendRequest( "/sample/echo?msg="+encodeURIComponent(msg) +"&err="+encodeURIComponent(err),  
  APIBridge.Internal._sendRequest(  
+
    null,
  "/sample/echo?msg="+encodeURIComponent(msg) +"&err="+encodeURIComponent(err),  
+
    function(req){
  null,
+
    if (req.responseText)
  function(req){
+
      onSuccess (req.responseText);
  if (req.responseText)
+
    else  
  onSuccess (req.responseText);
+
      onError(500);  
  else  
+
    },
  onError(500);  
+
  onError );
  },
+
  }
  onError );
+
}
  }
+
    }
+
 
+
 
</code>
 
</code>
 
 
  
  
Line 32: Line 50:
  
 
If you dissect the URL portion, you will see two parts:
 
If you dissect the URL portion, you will see two parts:
 
 
* '''Request''': <tt>/sample/echo.</tt> This request will get routed to your plug-in as specified in the ECOM Plug-in resource file.
 
* '''Request''': <tt>/sample/echo.</tt> This request will get routed to your plug-in as specified in the ECOM Plug-in resource file.
 
 
* '''Parameters''': <tt>?msg="<nowiki>+</nowiki>encodeURIComponent(msg)<nowiki>+</nowiki>"&err="<nowiki>+</nowiki>encodeURIComponent(err).</tt> The parameters are extracted at the servlet<nowiki>’</nowiki>s <tt>ServiceL</tt> function by using the <tt>RQueryParser</tt> class and this function:
 
* '''Parameters''': <tt>?msg="<nowiki>+</nowiki>encodeURIComponent(msg)<nowiki>+</nowiki>"&err="<nowiki>+</nowiki>encodeURIComponent(err).</tt> The parameters are extracted at the servlet<nowiki>’</nowiki>s <tt>ServiceL</tt> function by using the <tt>RQueryParser</tt> class and this function:
 
+
*: <code javascript> req-> GetRequest()->GetQuery()</code>
<tt>req-<nowiki>></nowiki>GetRequest()-<nowiki>></nowiki>GetQuery()</tt>
+
  
 
==How it is used==
 
==How it is used==
 
The code below shows how a Web Runtime (WRT) application might use this newly created function:
 
The code below shows how a Web Runtime (WRT) application might use this newly created function:
  
<code>
+
<code javascript>
 
+
Sample.echo(msg,err, function(text) //OnSuccess callback
  Sample.echo(msg,err,
+
   {
 
+
   output.innerHTML += "The  APIBridge returned: "+text+"<br/>";
  '''function'''(text) //OnSuccess callback
+
   },
    
+
   function(err) //OnError callback
  <nowiki> {</nowiki>
+
   {
 
+
   output.innerHTML += "Error: "+err.status + ". Message: "+err.responseText+"<br/>";
   output.innerHTML <nowiki>+</nowiki>= "The  APIBridge returned: "<nowiki>+</nowiki>text<nowiki>+</nowiki>"<nowiki><</nowiki>br/<nowiki>></nowiki>";
+
    
+
  <nowiki>}</nowiki>,
+
    
+
  '''function'''  (err) //OnError callback
+
    
+
  <nowiki>{</nowiki>
+
 
+
   output.innerHTML <nowiki>+</nowiki>= "Error: "<nowiki>+</nowiki> err.status <nowiki>+</nowiki> ". Message: "<nowiki>+</nowiki>err.responseText<nowiki>+</nowiki>"<nowiki><</nowiki>br/<nowiki>></nowiki>";
+
 
+
 
</code>
 
</code>
  
Line 67: Line 72:
  
 
===SendRequest function===
 
===SendRequest function===
<tt>APIBridge.Internal._sendRequest: '''function'''( path, parameters, onSuccess, onError )</tt>
+
<tt>APIBridge.Internal._sendRequest: '''function'''( path, parameters, onSuccess, onError )</tt>
  
 
This function is used to send a raw request to the APIBridge server; it takes care of security and access control for the developer.
 
This function is used to send a raw request to the APIBridge server; it takes care of security and access control for the developer.
  
{|border="2" cellspacing="0" cellpadding="4" width="100%"
+
{| class="wikitable"
|align = "center" bgcolor = "#B3B3B3"|'''Parameter'''
+
! Parameter  
|align = "center" bgcolor = "#B3B3B3"|'''Description'''
+
! Description
 
+
 
|-
 
|-
 
|'''path'''
 
|'''path'''
|URI path to the plug-in including any GET parameters. The <nowiki>http://localhost</nowiki> part should NOT be included here.
+
|URI path to the plug-in including any GET parameters. The '''<nowiki>http://localhost</nowiki>''' part should NOT be included here.
  
 
|-
 
|-

Latest revision as of 07:21, 7 November 2011

This article shows how to create APIBridge Binding Code in JavaScript, making it possible to access device services in Symbian WebRuntime widgets.

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

Contents


[edit] 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) {
APIBridge.Internal._sendRequest( "/sample/echo?msg="+encodeURIComponent(msg) +"&err="+encodeURIComponent(err),
null,
function(req){
if (req.responseText)
onSuccess (req.responseText);
else
onError(500);
},
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:
     req-> GetRequest()->GetQuery()

[edit] How it is used

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

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

[edit] JavaScript Binding API

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

[edit] 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.
This page was last modified on 7 November 2011, at 07:21.
60 page views in the last 30 days.
×