You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Structure of the Code

There are three types of JavaScript code:

  • Utilities
  • Schema
  • Service

Utilities

The utilities code is a fixed set of JavaScript that provides some browser compatibility and XML management. This code is delivered in the distribution in the file etc/cxf-utils.js. If you are using the ?js URL handler, it is delivered at the beginning of the response (unless you add ?nojsutils to the URL). If you are generating JavaScript using the tools, it is up to you to copy this file use it.

Schema Code

The Schema code generates one object for each 'bean' used in your service. This code is organized by XML Schema. The code for each schema starts with a comment like:

//
// Definitions for schema: http://apache.org/hello_world_soap_http/types
//  file:/home/benson/cxf/trunk/distribution/src/main/release/samples/js_browser_client/wsdl/hello_world.wsdl#types1
//

The generator generates a JavaScript constructor for each global complex type and element in the schema. Generally, you will find that the service methods are defined in terms of types, not elements. However, depending on whether you use Document or RPC, and depending on exactly how you configure your parts and types, you may find that a particular method is defined in terms of an 'element'-based JavaScript type instead of a 'type'-based class.

A typical JavaScript class for a type looks like:

function apache_org_hello_world_soap_http_types_sayHiResponse () {
    this._responseType = '';
}

function apache_org_hello_world_soap_http_types_sayHiResponse_getResponseType() { return this._responseType;}
apache_org_hello_world_soap_http_types_sayHiResponse.prototype.getResponseType = apache_org_hello_world_soap_http_types_sayHiResponse_getResponseType;
function apache_org_hello_world_soap_http_types_sayHiResponse_setResponseType(value) {this._responseType = value;}
apache_org_hello_world_soap_http_types_sayHiResponse.prototype.setResponseType = apache_org_hello_world_soap_http_types_sayHiResponse_setResponseType;

This is very simple type, derived from the return part of a Document/Literal service. It has one piece of data in it, called 'responseType'. Note that the code style here is to define getters and setters over 'private' properties. The code does not go to elaborate lengths to make the properties private; it just puts an _ on the front of the names.

Service Code

The code for a service starts with a comment, followed by a constructor for the per-service object:

// Javascript for {http://apache.org/hello_world_soap_http}Greeter

function apache_org_hello_world_soap_http_Greeter () {
    this.jsutils = new CxfApacheOrgUtil();
    this.synchronous = false;
    this.url = null;
    this.client = null;
    this.response = null;
    this._onsuccess = null;
    this._onerror = null;
}

There are two important properties defined here: *url* and *synchronous*. 

h2. URL

You are responsible for setting the url property with the URL of the web service. The generated JavaScript does not include any concept of services and ports. You simply put the appropriate URL into the property. *Note:* CXF's JavaScript clients don't support cross-scripting. If you want to cross-script, you have some choices:
* Stick with Mozilla/Firefox and sign the code.
* Modify the utils to use some of the common workarounds that permit cross-scripting in some browsers in some circumstances.

h2. Synchronous and Asynchronous processing

The CXF JavaScript code generator is designed to facilitate typical, AJAX, asynchronous processing. As described below, the per-operation functions take callbacks as parameters, and call them back when the server responds. If you want to use synchronous communications, you can set the 'synchronous' property to 'true'. That does not change the API, but rather changes the behavior. With this setting, the operation functions do not return until after the server has responded and the callbacks have been called back.



  • No labels