Creating APIBridge JavaScript Binding Code
Contents |
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()
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
<nowiki> {</nowiki>
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>";
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. |

