The functional behavior of your web application can be coded in your DataFlex server-side application by setting properties, creating methods and creating event handlers.
The first time when a client (Browser) calls the server to load a view, the server will send to the client a structure of data that describes the view. This structure will contain a nested list of all web objects in the view and the set of properties belonging to each web object. The client uses these to create the visual representation of each web object and set up the user interface.
While your web application is in use, various events are triggered. Writing code to handle these events is the main technique you will use to define the behavior of your web objects, and thus the behavior of your web application.
Each web object class has a specific event interface relevant to that class. Below are some examples of web object classes and events.
cWebView: OnShow, OnHide
cWebForm: OnPrompt, OnSetCalculatedValue
cWebButton: OnClick
Below is an example of how web properties can be used in the OnClick event of a cWebButton:
Object oLimitedClicks is a cWebButton
Set psCaption to "click me"
{ WebProperty=True }
Property Integer piCount 0
Property Integer piMaxCount 5
Procedure OnClick
Integer iCount iMaxCount
WebGet piCount to iCount
Get piMaxCount to iMaxCount
Increment iCount
If (iCount <= iMaxCount) Begin
Send ShowInfoBox ("Hello world wide web " + String(iCount))
WebSet piCount to iCount
End
If (iCount = iMaxCount) Begin
WebSet pbEnabled to False
End
End_Procedure
End_Object
In this example when the button is clicked on the client an OnClick event will be sent to the corresponding cWebButton object on the server. A web property is used to keep track of how many times the button has been clicked at the client. Once the pre-defined maximum is reached, the button’s pbEnabled web property is set to false.
By default, clicking on a button will always trigger the OnClick event in the server’s cWebButton object. In order to make web applications run efficiently, not all events of a web object’s event interface are automatically sent to the server.
Each event in a web object’s client-server event interface has a Boolean property that controls whether or not the client will send the event to the server. For a cWebButton’s OnClick event the controlling property is called pbServerOnClick. This value is true by default.
The cWebView class has an OnShow event that is triggered in the server object when the view is made active on the client. The controlling property for this event is pbServerOnShow and it is false by default. If you want a view object’s OnShow event to fire you must set pbServerOnShow to true.
In general, the name of an event’s controlling property is:
pbServer{event-name}
By default, most events in the web-object event interface do not fire on the server.
In addition to server-side event handling, you can also specify a client side event listener. Each event in the web object’s event interface has an associated property that can be set to the name of a JavaScript event listener on the client.
In general, the name an event’s client-side handler property is:
psClient{event-name}
For a cWebButton’s OnClick event the client-side listener property is called psClientOnClick. Below is an example of how you might declare a cWebButton with a client-side event listener:
Object oMyButton is a cWebButton
Set psCaption to "Click Me"
Set psClientOnClick to "oMyButtonClick"
Set pbServerOnClick to False
End_Object
In the above code when oMyButton is clicked on the client the server-side OnClick event is not sent but a JavaScript function “oMyButtonClick” is called. In order to make this button work you would also need to implement the JavaScript function.
Client-side event handling is an advanced technique that requires good knowledge of the JavaScript programming language.
Web Methods are used to define a web object’s external interface. A method declaration becomes a web method by publishing it as a web published method. Only subclasses of the cWebObject class understand the interface for publishing a web method.
For security reasons, only a web object’s web methods may be directly called by the client. Each method of a web object’s client-server event interface is a published web method.
Normally, you do not need to web publish a method declaration unless you are writing your own JavaScript class that needs to call messages on the server. One case where you will need to publish a web method is when you declare a callback method for a particular operation, i.e. when you pass the name of a web object’s web method to the client so the client can call-back this method at some later time.
A method can be published as a web method using the WebPublishProcedure command for procedures and the WebPublishFunction command for functions. In an object declaration you would publish your method after the method declaration. In a class declaration you must publish your methods inside End_Construct_Object before Forward Send End_Construct_Object.
The following example demonstrates how to publish a web method in an object declaration.
Object oConfirmBtn is a cWebButton
Set psCaption to "Confirm"
Procedure ConfirmResponse Integer iResult
WebSet pbEnabled to (iResult <> 1)
End_Procedure
// Publish the response method
WebPublishProcedure ConfirmResponse
Procedure OnClick
Send ShowMessageBox of oWebApp (Self) (RefProc(ConfirmResponse)) ;
"Do you want to disable this button?" "Question title!"
End_Procedure
End_Object
In this example the procedure ConfirmResponse is declared and web published. In the object’s OnClick event the ShowMessageBox client message is sent passing a reference to the ConfirmResponse web message. When the message box is closed on the client it will call back ConfirmResponse on the server passing the iResult parameter.
Previous Topic: Web Properties
Next Topic: Web Application Flow