Example application

This section gives a detailed example of ZOO-Client based JavaScript appliclation.

Note

For this example application, first setup a /zoo-client-demo directory accessible from your web server at http://localhost/zoo-client-demo.

The following subdirectories must be created in the /zoo-client-demo directory:

assets
assets/js
assets/js/lib
assets/js/lib/hogan
assets/js/lib/jquery
assets/js/lib/query-string
assets/js/lib/xml2json
assets/js/lib/zoo
assets/tpl

You will need to copy your node_modules javascript files copied in the hogan and query-string directories. First, you wil need to install query-string.

npm install query-string

Then you will copy query-string.js and hogan-3.0.2.js files in your zoo-client-demo web directory. Those files are located in your ~/node_modules directory.

For other libraries, you will need to download them from their official web sites and uncompress them in the corresponding directories.

Loading the modules from your web application

Before using the ZOO-Client, you will first have to include the javascript files from your web page. With the use of requirejs you will need only one line in your HTML page to include everything at once. This line will look like the following:

<script data-main="assets/js/first" src="assets/js/lib/require.js"></script>

In this example, we suppose that you have created a first.js file in the assets/js directory containing your main application code. First, you define there the required JavaScript libraries and potentially their configuration, then you can add any relevant code.

 1requirejs.config({
 2    baseUrl: 'assets/js',
 3    paths: {
 4        jquery: 'lib/jquery/jquery-1.11.0.min',
 5        hogan: 'lib/hogan/hogan-3.0.2',
 6        xml2json: 'lib/xml2json/xml2json.min',
 7        queryString: 'lib/query-string/query-string',
 8        wpsPayloads: 'lib/zoo/payloads',
 9        wpsPayload: 'lib/zoo/wps-payload',
10        utils: 'lib/zoo/utils',
11        zoo: 'lib/zoo/zoo',
12        domReady: 'lib/domReady',
13        app: 'first-app',
14    },
15    shim: {
16        wpsPayloads: {
17            deps: ['hogan'],
18        },
19        wpsPayload: {
20            deps: ['wpsPayloads'],
21            exports: 'wpsPayload',
22        },
23        hogan: {
24            exports: 'Hogan',
25        },
26        xml2json: {
27          exports: "X2JS",
28        },
29        queryString: {
30            exports: 'queryString',
31        },
32    },
33});
34
35requirejs.config({
36    config: {
37        app: {
38            url: '/cgi-bin/zoo_loader.cgi',
39            delay: 2000,
40        }
41    }
42});
43
44require(['domReady', 'app'], function(domReady, app) {
45    domReady(function() {
46        app.initialize();
47    });
48});

On line 2, you define the url where your files are located on the web server, in assets/js. From line 3 to 14, you define the JavaScript files to be loaded. From line 15 to 21, you configure the dependencies and exported symbols. From line 35 to 42, you configure your main application.

In this application, we use the domReady module to call the initialize function defined in the app module, which is defined in the first-app.js file as defined on line 13.

 1define([
 2    'module','zoo','wpsPayload'
 3], function(module, ZooProcess, wpsPayload) {
 4
 5    var myZooObject = new ZooProcess({
 6        url: module.config().url,
 7        delay: module.config().delay,
 8    });
 9
10    var initialize = function() {
11        self = this;
12        myZooObject.getCapabilities({
13           type: 'POST',
14             success: function(data){
15                     console.log(data);
16            }
17        });
18
19        myZooObject.describeProcess({
20            type: 'POST',
21            identifier: "all",
22            success: function(data){
23                console.log(data);
24            }
25        });
26
27       myZooObject.execute({
28           identifier: "Buffer",
29           dataInputs: [{"identifier":"InputPolygon","href":"XXX","mimeType":"text/xml"}],
30           dataOutputs: [{"identifier":"Result","mimeType":"application/json","type":"raw"}],
31           type: 'POST',
32           success: function(data) {
33                console.log(data);
34           },
35           error: function(data){
36                console.log(data);
37           }
38        });
39    }
40
41    // Return public methods
42    return {
43        initialize: initialize
44    };
45
46});

On line 5 you create a “global” ZooProcess instance named myZooObject, you set the url and delay to the values defined in first.js on line 35. From line 10 to 40, you define a simple initialize function which will invoke the getCapabilities (line 12 to 18), describeProcess (from line 20 to 26) and execute (from line 28 to 39) methods. For each you define a callback function which will simply display the resulting data in the browser’s console.