Commonly Used Classes
TIBCO General Interface has a very rich set of features, and its framework includes numerous classes and methods. Product documentation includes a comprehensive API reference document similar to that available for Java. This section presents some of the more commonly used classes and APIs with specific use cases and sample code snippets.
Application Controller
General Interface has a high-level application controller that orchestrates various functions in a General Interface application. A single instance of this controller is created for every application. The name of this global controller class in General Interface is jsx3.app.Server. It is used frequently in custom code because it allows access to other parts of the same application. For example, from an event handling code in a Select component, if you need to call a method on the grid component of the same application, you could use the following:
this.getServer().findJSXByName('mygrid').getSelection();
In this statement, this refers to the instance of Select control that is making the call. If you are a Java or C++ developer, it's the same this pointer that is used to refer to the current instance of a class. Refer to Chapter 7 for details on writing custom classes in General Interface.
Cache
Cache is the global memory space that is available to an application with smart management similar to Java's garbage collection. In this class, General Interface has provided a very smart mechanism for storing data within the browser that can be accessed by any other function or at a later time. This class is also used to keep some key data for some of the controls. For example, the grid control keeps its backing data in an instance of the jsx3.app.Cache class. Applications can use this to store larger result sets locally in the client memory. However, care must be taken to not load too much into the client browser and avoid a memory overload of IE (or Firefox).
Custom Classes in JavaScript for General Interface
The exhaustive set of functions available in General Interface's framework makes it possible to write complex logic in a very few lines of JavaScript code that can also be embedded within the properties of controls similar to event handlers like onClick in HTML elements. JavaScript that accompanies any General Interface application can be written in the usual way in files with the .js extension. When you start a new project in General Interface, it opens a file logic.js in the workspace where small JavaScript scriptlets to handle event handling or validation logic can be placed. However, the best practice when writing JavaScript for General Interface is to develop JavaScript classes similar to Java classes. Each .js file should have only a single class defined in it. The way to define a class in General Interface is to use the defineClass() function from the jsx3.lang package. Listing 2.1 shows partial source code for a class written using TIBCO General Interface's framework:
Listing 2.1. Custom Class Defined in General Interface
/* File: CommandHandler.js * Description: Implements a command handler for online * equity trading application * * This class follows Command pattern to implement * a generic handler for all menu items from * the main trading window */ jsx3.lang.Class.defineClass ( "com.tibcobooks.gi.chapter2.CommandHandler", // name of class com.tibcobooks.gi.chapter2.BaseHandler, // similar to "extends" part of Java class definition [], // Similar to "implements" part of Java class definition function ( CommandHandler ) { CommandHandler.prototype.buyURL = 'http://www.anilgrnani.com/gibook/chapter2/by'; CommandHandler.prototype.sellURL = 'http://www.anilgrnani.com/gibook/chapter2/sell'; CommandHandler.prototype.init = function() { // this is the constructor in General Interface framework }; } );
This framework can be used independently of General Interface by simply including some of the JavaScript files from the General Interface distribution. General Interface framework is open source, so there is no licensing nor cost implication of using it this way. It certainly makes JavaScript code much more modular and therefore easy to maintain.
The process of loading the class essentially downloads the JavaScript source file and executes it using the JavaScript method evaluate() to insert it dynamically into the current set of available JavaScript files. After the class has been loaded, other parts of the program can create instances of the class, or they may call any static methods or access static properties by directly referencing it with the fully qualified class name—for example:
com.tibcobooks.gi.chapter2.CommandHandler.sellURL = 'http://www.anilgrnani.com/gibook/chapter2/newSellURL'