[Contents] [Previous] [Next] [Last]

Chapter 5
Adding Behavior to an Application

This chapter explains how to specify the behavior of your application by making connections between components based on user events, by creating JavaScript expressions to specify property values in JavaScript components, and by creating SQL queries to retrieve data from a database. The chapter also describes how to connect a form to a page that uses its values and how to add your own JavaScript code to your application.

You can determine the way your application responds to user events and changes in data by using the Connection Builder to make connections between components. See "Connecting Components."

The behavior of components is often determined by the values of their properties. Because the Inspector's property value cell provides only limited editing capability, custom property editors are available to edit more complex values.

To determine your application's response to user input, you can connect the form to a page that handles the submitted values. See "Connecting Forms."

Finally, if you want to specify behavior that does not fall into any other category, you can add JavaScript code directly to your application using special code components. See "Adding Code to an Application."

Connecting Components

When the end user takes an action, such as clicking a button or check box, entering text, or moving the cursor into or out of a text field, a program action called an event is generated. The behavior of your application depends on what actions are performed in response to these events. For example, when the user clicks a button labeled Next, the application might respond by getting and displaying the next record.

From the point of view of the programmer, an event handler function is called when a click event occurs, and responds by invoking another method that downloads new data and updates the table. From the point of view of the user, however, clicking the button updates the table. The button component and the table component are connected through the click event.

The key elements of any interaction are the source event and the action. The source event is the originator of the action; it determines when to execute the code. You can add conditions to an interaction just as you would add an if statement in code. The action specifies what to do when the condition is satisfied.

One of the most powerful features in Visual JavaScript is the ability to specify interactions at a high level, between components. Using the Connection Builder you can easily and quickly create connections, or interactions, between components, thereby specifying the behavior of your application without actually writing any code at all. Visual JavaScript automatically generates client-side JavaScript code for the specified relationship

NOTE: The Connection Builder makes only client-side connections. Because the connections are implemented in client-side JavaScript, the end-user of an application that contains connections must use a JavaScript-enabled browser.
You can specify connections between server-side components by setting property values that are component references or JavaScript expressions. See "Creating JavaScript Expressions."

Connection Types

Depending on the kinds of components you are connecting, there are three types of connections you can make: property connections, direct connections, and event connections.

All connections are implemented by generated client-side JavaScript code.

Specifying Connections

In the Connection Builder (Figure 5.1), you can edit an existing connection for the current component, or you can create a new connection. To create a new connection, select "Create new connection." If you want to edit an existing connection, select one from the drop-down list. (See "Examining and Modifying Connections" for more information on modifying existing connections.)

Figure 5.1    The Connection Builder

The Source Component field shows the component where the action originates. This could be, for example, a button that the user clicks, or a database cursor whose data can change. The Source Component reflects the currently selected object in the Page Editor, in the same way that the Inspector does. If you select a new object in the Page Editor while you are creating or editing a connection, you are warned that the source component will change and any currently specified connection information will be discarded.

The lower section of the window specifies the action that is taken in response to the source event. This section is different for each connection type, as discussed later.

When you have made your choices, click Apply to create the connection. When you click Done, the Connection Builder disappears. If the current specification has not yet been applied, the information is discarded.

When a connection is applied, Visual JavaScript generates client-side JavaScript code in the body of the page containing the source component. The code is run when that page is loaded, and in some cases, when the trigger event occurs.

Specifying a Direct Connection

When you specify a direct connection type, the Connection Builder appears as shown in Figure 5.2.

Figure 5.2    The Connection Builder building a direct connection

The Target Component field shows the name of the target component. This is the component that displays the result of the action, such as the form that displays new data. If you invoked the Connection Builder by dragging the connection plug to a target component, that component appears here. You can select a different target component from the drop-down list.

For a direct connection, the name of the target component is all you need to specify. Everything else is automatic.

For direct and property connection types, a check box (initially, unchecked) lets you specify whether the connection should be two-way. In a two-way connection, each component acts as both source and target, just as if you had specified a second connection with the source and target reversed.

Specifying a Property Connection

When you specify a property connection type, the Connection Builder appears as shown in Figure 5.3.

Figure 5.3    The Connection Builder building a property connection

The Target Component field shows the name of the target component. This is the component that changes as a result of the change in the Source property. If you invoked the Connection Builder by dragging the connection plug to a target component, that component appears here. You can select a different target component from the drop-down list.

For a property connection, you must select the property to be set in the target component. This is most commonly the Value property, which is selected by default if present.

For direct and property connection types, a check box (initially, unchecked) lets you specify whether the connection should be two-way. In a two-way connection, each component acts as both source and target, just as if you had specified a second connection with the source and target reversed.

Specifying an Event Connection

When you specify an event connection type, the Connection Builder appears as shown in Figure 5.3.

Figure 5.4    The Connection Builder building an event connection

For an event connection, you must specify a JavaScript event handler, which is called in response to the source event. You can enter JavaScript code directly in the script handler field, or click Script Builder to display the Script Builder tool. The Script Builder, shown in Figure 5.5 helps you to generate JavaScript code.

Figure 5.5    The Script Builder

Follow these step to use the Script Builder:

  1. Click Script Builder in the Connection Builder.

  2. Choose a target component from the Component drop-down list. This is a component with a method to be called or a property to be set in response to the source event; for example, a cursor component containing a method to be called. (Note that the list of target components includes the standard client-side JavaScript runtime objects window and document.)

  3. Choose the kind of action to be taken: Call Method or Set Property.

  4. A dialog box appears in which you can specify the value to which the chosen property is to be set, or the parameters to pass to the chosen method. When you have made the required specifications, click OK to add the chosen action to the script.

  5. If you want, you can add additional statements to the event handler script in the same way. You can choose another method or property in the same component, or choose another target component.

  6. When the script is complete, click OK to return to the Connection Builder. The script you have built appears in the script handler field, where you can edit it if you wish.

Examining and Modifying Connections

You can examine existing connections in any of the following ways:

Choose from the list of existing connections to display one, which you can modify or delete. The source component and event of an existing connection are fixed. You can make the following kinds of modifications to existing connections:

Click Delete to remove an existing connection from the application.

NOTE: The source component in the Connection Builder is always the currently selected component. If you select another component in the Page Editor while you are in the process of modifying a connection, a dialog box warns you that the currently displayed connection information will be discarded if you change the source component. If you choose to continue (that is, change the source component), the connection you were modifying reverts to its most recently applied state. §

Creating JavaScript Expressions

In JavaScript components, you can initialize properties with JavaScript expressions, which are evaluated to derive the property value. You generally use an expression to calculate a numeric value, derive a value from a property value in another object, or build a string value using these kinds of derived values.

In the Inspector, a property that can have an expression value has an equal-sign (=) button on the right side. A value that is currently specified by an expression also has an equal-sign at the left side of the value cell. You can enter a JavaScript expression directly into the value cell in the Inspector by prefacing the value with an equal-sign and enclosing any string values in quotes, as in this example:

= "User entered: " + Text1.Value 
Alternatively, you can use a custom property editor called the Expression Editor (Figure 5.6) to help you build and enter a JavaScript expression. Click the equal-sign button at the right of the property's value cell to bring up the Expression Editor.

Figure 5.6    The Expression Editor

The Expression Editor displays a list of available objects on the left.

When you select one of the objects, its properties appear in the list on the right.

Follow these steps to build an expression:

  1. Type directly into the text box at the bottom of the window. Enclose string values in quotes, and enter numbers and operators directly.

  2. Double-click a property in the right-hand list to add a JavaScript property accessor expression to the text box. For example, if you select the request object and double-click the _name property, request._name appears in the text box at the cursor location. At run time, this expression is evaluated to derive the value of the property.

  3. Use the plus operator (+) between strings to concatenate them, or to concatenate expressions and strings. (Between numbers, the plus operator indicates addition.) You can also use any other built-in JavaScript functions or operators to create a valid JavaScript expression.

  4. When the expression is correct in the text box, click Set Value.
    Tip The values submitted by a form are passed as parameters to the associated page via the request object. When you have interactively connected an HTML form to the current page (as described in "Connecting a Form to a Page" on page 94), you can see and access these runtime values in the Expression Editor when you are setting the value of a server component property. §

Creating SQL Queries

The SQL Query property of database-access components must contain a valid SQL select statement in order for the component to access data from the database. You can use a custom property editor called the SQL Query Builder to help you build and enter a valid SQL statement.

Important If you are using Enterprise Server 2.x, you must use Application Manager to install the following file to your web server before attempting to use the SQL Query Builder: install dir/serverJS/metadata2.web. See the Release Notes for instructions on installing this application. §
A number of components have a SQL Query property, in which you specify a select statement with which to download data for display. This property is in the following components:

Figure 5.7 shows the SQL Query property of a JavaScript Client Cursor component.

Figure 5.7    Inspecting a SQL Query property in the JavaScript Client Cursor component

When you inspect one of these components, click the ellipsis (...) at the right of the SQL Query property's value cell to bring up the SQL Query Builder custom property editor (Figure 5.8). If you have not yet connected to a data source, you are prompted for your design-time user name and password before the SQL Query Builder appears.

Figure 5.8    The SQL Query Builder custom property editor

Follow these steps to build a SQL Query using the SQL Query Builder:

  1. Select the Database tab to specify the data source you will be using. You must identify a data source before you can build an SQL statement that selects particular tables and columns.

  2. Select a data source from the drop-down list. This list shows data sources that have already been identified. If the data source you want to use is not listed, click Create to add it, then select it from the list.

    Note: When you identify a data source, the DBPool property of the component you are editing is set. If a DBPool component for the data source is found on the globals.html page, it is used. Otherwise, a new DBPool component is created on the globals.html page. For compatibility with earlier releases, you also have the option of using the DBPool already specified in the DBPool property. §

  3. Select the Query tab to specify the SQL query. The Query tab is shown in Figure 5.9.

    Figure 5.9    The Query tab of the SQL Query Builder

If you have successfully identified a data source (and your database server is correctly configured), the tables from that data source appear on the left. When you select a table, its columns appear in the middle. Available operations appear on the right.

The text box near the bottom holds the query you are building. The clauses appear separately in their own boxes and are automatically added to the query.

There are a number of options for building queries:

Click Clear to remove all values and begin again. When the you have built a valid SQL statement, click Set Value.

NOTE: The SQL expression is stored in the SQL Query property as a string value. If you want to include any quoted strings in the expression, you must use the backslash (\) escape character before internal quote marks, as in this example:
"SELECT ITEM_NUM \"Product ID\", DESCRIPTION \"Product Description\" FROM ITEM_MASTER" 

Adding an Expression to a SQL Query

If you wish to add a JavaScript expression to the query, build the rest of the query, then select the Expression Editor tab. This is the same as the Expression Editor that allows you to enter expressions as property values. It shows you available server objects and their properties. When you double-click a property, an accessor for that property replaces the highlighted text in the query. If no text is selected, the accessor is added to the end of the query.

For example, when you submit a form, the target document can reference the form values using a JavaScript expression as follows:

var myVar = request.myVal; 
When the target document is a Visual JavaScript page that contains a cursor and an associated SQL statement, you can modify that statement to include values passed to the page from a calling form as follows:

"SELECT myFirstColumn, mySecondColumn FROM myTableWHERE myFirstColumn =
'" + request.myVal + "' "

Connecting Forms

You can connect form elements to other components (such as database cursors) to retrieve data or display values, and you can also connect the form itself to another page, so that page can receive and process the values submitted with the form. This feature is often used when the form is part of a database application, and the values entered in the form must be submitted to the database.

See "Creating Forms to Display Data" for more information on connecting form elements so that they display data from a database.

When you call the submit method of an HTML Form component, the values of all the elements on the form are collected and sent as parameters to the URL that is the value of the Form's Action property. The Form's Method property determines if the parameters are sent in the same transmission (GET) or in a separate transmission (PUT).

You can place a Submit Button component on the form to make it easy for users to apply values they have specified. The Submit Button component is a specialized HTML button that, when clicked, calls the containing form's submit method. The label of the button is, by default, Submit. You can change the label by inspecting the component and setting the Button Label property.

You can connect an HTML Form component to another page so that when the form is submitted, the connected page is loaded and the values of the form's elements are passed as parameters to that page. The connected page can contain server-side processing components, such as the Database Form Handler, that use the parameters submitted with the form.

See "Creating Forms to Display Data" for more information on passing values to a database using the Database Form Handler component.

Connecting a Form to a Page

You can connect a form to a page by using the Inspector to set the value of the Action property directly to the URL of the target page. You can specify an absolute URL (an entire path starting with http://) or a relative URL (a page file name that can be found in the project directory).

You can also connect a Form component to another page in the same project interactively, using drag and drop. Follow these steps to do so:

  1. Display both the page containing the Form and the target page in Page Editor windows. Arrange the windows so that you can see both pages at once, with the page containing the Form on the right.

  2. Click on the connection box in the upper-right corner of the Form component.

  3. Hold down the button and move the cursor. The cursor changes to the "plug" icon to indicate that you are in connection mode.

  4. Drag the connection cursor to a blank area on the target page, making sure that no component is selected. Release the button.
When the connection is made, the value of the Action property of the Form component is set to the URL of the target page. The target page should contain a server component, such as Database Form Handler, that uses the form values. When the user submits the form, the target page is loaded, and the values in the form are passed through the request server object.

At runtime, the request object has a property for each value, named for the form element whose value is being passed, preceded by an underscore--for example, _Text1.

NOTE: If you have connected the form to the page interactively (by dragging and dropping the plug icon) you can use the Expression Editor to access the runtime properties of the request object that contain the form values. If you have connected the form to the page by setting the Action property directly, however, these value-access properties do not appear on the request object until run time. To access them from the action page, you must type in the generated property names. §

Using the Form Parameters

When you submit a form, the values of the elements on the form are passed to the server as parameters of the request server object. A page receiving the request can use these values by accessing properties of the request object that are named for the form elements.

When you have connected the form to a target page by dragging the connection plug from the form to the page, you can see the value-passing properties of the request object in the Expression Editor. (Note that this is not the case when you specify the URL of the target page directly in the Action property of the form.) When you edit a JavaScript-valued property of a component on the target page, the Expression Editor allows you to retrieve the form values from the request object.

Follow these steps to retrieve form values:

  1. Display the target page in the Page Editor.

  2. Display the server component (such as Database Form Handler or Send Mail from Server) in the Inspector.

  3. Select a property that should use one of the values submitted with the form. This must be a JavaScript-valued property, as indicated by the equal-sign (=).

  4. Click the equal-sign (=) to bring up the Expression Editor.

  5. In the Expression Editor, click the request object. Its properties appear in the right-hand list. If you have connected the form and page interactively, the list includes the runtime properties with the names of the form elements (such as _Text1).

  6. Double-click a property name. The JavaScript that accesses the value is placed in the Expression text box.

    Note that the runtime properties appear in the list only if the form has been connected to this page interactively. If the form has not yet been connected, or if you made the connection by setting the form's Action property directly, type the property accessor directly into the Expression text box--for example, request._Text1.

  7. You can edit the expression if you wish. For example, you can double-click another property and place a plus sign between the two accessors to concatenate the values, or concatenate property values to strings.

  8. Click Set Value to enter the completed expression as the value of the property you are editing.
See "Creating JavaScript Expressions" for more information.

Adding Code to an Application

There are several places where you can enter JavaScript code into your application:

For advanced application development, you can add arbitrary JavaScript code to an HTML page by putting a code component on it. Special components are available to contain the client-side or server-side code:

These components are not visible in the end-user interface and do nothing by default. Each of the components has a JavaScript Code property, which contains the code that runs when the page is loaded. To set the value, click the ellipsis (...) next to the JavaScript Code property in the Inspector. A multi-line text editor appears, in which you can enter or edit JavaScript code.

For the Client Script component you can specify, in the Source File property, a file containing JavaScript code. This code is run when the page is loaded. If you specify a source file, the JavaScript Code property is not considered.

You can use script components to modularize your application logic as it becomes more complex. They can, for example, contain initialization or helper functions that you can call in the code fragments that you specify with the Script Builder and Expression Builder, making these code fragments more manageable.


[Contents] [Previous] [Next] [Last]

Last Updated: 11/06/97 13:24:52


Copyright © 1997 Netscape Communications Corporation