General Interface is an open source project hosted by the Dojo Foundation

Generating Function Code

A rules file defines the interaction between your application and the data service. Once the rules file is complete, the application also needs to invoke the service. The XML Mapping Utility generates the function code that invokes the service, which you add to your project in an included JavaScript file.

Recall that in one of the last steps of the "Creating an Application" tutorial in General Interface Getting Started, you configured the Find City and State button to execute the JavaScript function eg.service.callReturnCityState();. In this section, you generate the function code and add it to your project.

To generate the function code that invokes the web service, complete these steps:

  1. Click the Generate button next to the Delete button in the XML Mapping Utility and choose ReturnCityState from the drop-down menu in the Rules Tree panel menu. Resize the Rules Tree panel if you don't see the button.
    The JavaScript code for invoking this operation is copied to the clipboard. The code defines a JavaScript function that creates a service instance. The service instance uses the rules file to create the XML request document as well as process the service's XML response document.
  2. Click OK in the Code Generator prompt, which displays a message that the JavaScript code has been copied to the clipboard.
  3. Click the Minimize button in the XML Mapping Utility.
  4. Click the logic.js tab in the General Interface Builder work area.
    This file contains business logic for your application. It should be empty except for a comment line.
  5. Delete the comment line and press Ctrl+v to paste the contents of the clipboard into logic.js.
    The JavaScript code in the logic.js file should look like the following:
    jsx3.lang.Package.definePackage(
       "eg.service",
       //the full name of the package to create
       function(service) {
       //name the argument of this function
    
       //call this method to begin the service call
       //(eg.service.callReturnCityState();\)
    
          service.callReturnCityState = function() {
             var objService = myAddressLookup.loadResource("GetCityandState_xml");
             objService.setOperation("ReturnCityState");
    
             //subscribe and call
             objService.subscribe(jsx3.net.Service.ON_SUCCESS,
                service.onReturnCityStateSuccess);
             objService.subscribe(jsx3.net.Service.ON_ERROR,
                service.onReturnCityStateError);
             objService.subscribe(jsx3.net.Service.ON_INVALID,
                service.onReturnCityStateInvalid);
             objService.doCall();
          };
    
          service.onReturnCityStateSuccess = function(objEvent) {
          //var responseXML = objEvent.target.getInboundDocument();
          objEvent.target.getServer().alert("Success",
             "The service call was successful.");
          };
    
          service.onReturnCityStateError = function(objEvent) {
             var myStatus = objEvent.target.getRequest().getStatus();
             objEvent.target.getServer().alert("Error","The service call failed.
                The HTTP Status code is: " + myStatus);
          };
    
          service.onReturnCityStateInvalid = function(objEvent) {
             objEvent.target.getServer().alert("Invalid","The following message
                node just failed validation:\n\n" + objEvent.message);
          };
    
       }
    );

    After the Find City and State button is clicked and the web service is called, the function displays an alert dialog containing the text The service call was successful.

  6. Right-click the logic.js tab and select Save and Reload.
    This command saves the file with the modified contents and also loads the file into memory, making the methods available without requiring the browser window to be refreshed.

Contents

Searching General Interface Docs

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.