Dreamweaver MX Extensions
This part of the API is complex and is distinctly different from anything you've seen beforeto accommodate the different languages and structures the server markup can have. The good news is that unless you want to do something fancy, you don't have to code your server behavior extensions by hand. Dreamweaver provides a special interface, the Server Behavior Builder (SBB), to do the coding for you. In this chapter, we'll take a look at working with the builder; then we'll see what's really going on "under the hood" for those occasions when you want to tweak the coding yourself.
note Standard extensions such as objects and commands can be used to insert server markup. The advantage of coding your custom markup snippets as server behaviors is that you take advantage of the Dreamweaver management system. For instance, your behavior will appear as a menu choice only if the user is working with the appropriate server model and language (for example, ASP/JavaScript).
Server Behavior Builder
Even fairly complex server-side code can be built into an extension without handcoding, by using the SBB. The basic procedure is simple:
Some Basic Server Behavior Concepts
Even if you're using the Server Behavior Builder to do all the dirty work, you need to know a few fundamentals of how server behaviors work. These fundamentals will be even more important if you decide to code your own behaviors directly. The following list describes some of these elements, and Figure 9.2 shows how these elements will be added to the SBB main dialog box. Figure 9.2. The SBB dialog box. Multiple code blocks, parameters, and position/ weighting are set here.
Building Server Behaviors: Practice Session
To see how the SBB works, we'll create a few very simple server behaviors. For the examples shown here, we'll insert various pieces of ASP/JavaScript markup. Feel free to substitute code from another server model or language, if you prefer. Task 1: Create a server behavior that inserts a "Hello, world" message For our first behavior, we'll use a simple statement that inserts a line of code: <%="Hello, world!"%> To make things simple, we won't allow for any user inputthis behavior will always insert exactly the same hello message. Before you can proceed with this task, you need to activate the Server Behaviors panel by defining a dynamic site and opening a dynamic document. Take a moment to do that now, using ASP/JavaScript as your server model. (If you want to, follow the example code shown here.)
Task 2: Create a server behavior that inserts a user-defined message Next, we'll create a behavior that uses a variable to insert a user-specified message into the document. The finished code will consist of two code blocks. Above the <html> tag, the behavior will add the following code (shown in ASP/JavaScript): <% var myVar=" @@param1@@ " %> (The @@param1@@ value will be replaced by the custom message the user chooses when he inserts the behavior.) At the cursor, the behavior will insert the following: <%=myVar%>
Beyond the SBB: How Server Behaviors Are Constructed
When you use the SBB, extension files are being manipulated and created for you. If you want to move beyond the confines of the SBBor if you just enjoy knowing what's happening "under the hood" as you workyou'll want to examine the server behavior API in more detail. Server Behaviors in the Configuration Folder
Each server behavior is constructed from three or more files stored in the Configuration/Server Behaviors folder, in the subfolder corresponding to the appropriate markup language (ASP, JSP, ColdFusion, and so forth).
note On multiuser systems, these files are stored in the userspecific Configuration folder.
HTML/JS
Like all extensions covered so far, server behaviors begin with an HTML file. The HTML file, with an optional linked JS file, contains instructions for constructing and inserting the server markup into the user's document. XML
Unlike other extension types covered so far, the API for server behaviors separates the actual content to be insertedthe code blocksfrom the instructions on how to insert them. This content is stored in XML files with the .edml filename extension. For each behavior, the following exist:
Why put the code in XML files? Separating form from content like this allows Dreamweaver to efficiently manage similar server behaviors in each of the multiple server languages it supports. Figure 9.11 shows the Server Behaviors/ASP_JS folder with the various Hello World and Custom Message files in place. Note that each behavior consists of one HTML file (Hello world.htm and Custom Message.htm). In addition, the first behavior consists of two XML files: a group file for the behavior as a whole (Hello World.edml) and one participant file named after the only code block the behavior inserts (Hello World_block_1.edml). The second behavior consists of three files: a group file (Custom Message.edml) and two participant files for the behavior's two code blocks (Custom Message_block_1.edml and Custom Message_block_2.edml). Figure 9.11. The Configuration/Extension Data folder showing the XML data files for the Hello World and Custom Message behaviors.
Structure of a Server Behavior (XML)
The actual code to be inserted in the behavior is specified through custom tags and attributes in the group and participant XML files. To see the tags in action, examine the XML files for Hello World and Custom Message. For a complete listing of what the tags and attributes mean, check out Chapter 13 of the Extending Dreamweaver manual. Structure of a Server Behavior (HTML/JS)
A server behavior's HTML/JS files must contain various functions and other API elements in order to work properly. (To see these elements at work, examine the files created by the SBB for Hello World and Custom Message.) Page Title
Whatever appears in the document's <title> tag will become the + menu entry for the behavior and the title for any dialog box the behavior calls up. canApplyServerBehavior(sbObj) Function
This function, which is part of the API and is called automatically, determines whether the behavior can be inserted into the user's document. It requires one argumenta JavaScript object representing the behavior to be insertedwhich Dreamweaver passes to it automatically. It returns true or false . applyServerBehavior(sbObj) Function
This function, also part of the API and called automatically, inserts whatever code is specified in the XML files into the appropriate places in the user's document. It, too, is automatically passed one argumenta JavaScript object representing the behavior to be inserted. It relies on the applySB() helper function to perform the actual code insertion. (See the explanation of helper functions later in this chapter.) findServerBehaviors() Function
This function is called automatically whenever a document containing an instance of the server behavior is opened. It returns an array of JavaScript objects representing all instances of the current behavior in the document. It relies on the findSBs() helper function to perform the actual search. (See explanation of helper functions later in this chapter.) deleteServerBehavior(sbObj) Function
This API function is called automatically when the user clicks the button in the Server Behaviors panel. It is automatically passed a JavaScript object representing the behavior as it appears in the document. Its purpose is to delete the behavior. It relies on the deleteSB() helper function to perform the actual deletion. (See explanation of helper functions later in this chapter.) analyzeServerBehavior(sbObj,allRecs) Function
This optional API function, if present, is called automatically, in conjunction with findServerBehaviors() , to populate the list of applied behaviors in the Server Behaviors panel. When called, it is automatically passed two parameters: a JavaScript object representing the behavior and an array of records that constitute the data sources for the document. Its purpose is to remove duplicates and other unnecessary items from the behaviors list. Form
As with any extension, the dialog box for collecting user input is created from a form in the HTML file. The OK and Cancel buttons will be supplied automatically. The form is optional. inspectServerBehavior(sbObj) Function
This optional API function, if present, is called automatically when the user doubleclicks on a behavior in the Server Behaviors panel's list. It is automatically passed a JavaScript object representing the behavior as it appears in the document. Its purpose is to repopulate the behavior's dialog box so the user can edit any parameters. displayHelp() Function
If this optional API function is present, any dialog box will include a Help button. Helper Functions
Back in Chapter 2, "Creating Custom Objects," in introducing the Dreamweaver API, we distinguished between functions that are part of an extension type's API procedure and those that are part of the extension-writing API, but not an automatic part of any process. The former type of function is called automatically, but must be individually scripted to return the required values. The latter type must be explicitly called, but is already scripted to return appropriate values. All of the API functions listed previously are of the former type. But several of them call on helper functions of the latter type. The helper functions allow you to encode server behaviors with functions that are automatically called, which then call helper functions that are already scripted, and return their values. This can leave very little for you to do. The following code, for instance, finds all instances of a particular behavior in a document, without you doing much more than passing control to a helper function and returning its value: function findServerBehaviors() { var sbObj=findSBs(); return sbObj; } Helper functions are unique in that each works only within the API function whose name it matches findServerBehaviors() and findSBs() , for instance. Table 9.1 lists the specifications of the helper functions. Table 9.1. Server Behavior Helper Functions
API Procedure for Server Behaviors
Figure 9.12 diagrams the API procedure for processing server behaviors. As the diagram shows, various processes can occur, initiated by the user adding, editing, duplicating, or deleting behaviors, or opening documents containing behaviors. The diagram also shows the importance of the JavaScript object array, created by findServerBehaviors() and accessed by other functions. It also shows the interrelationship between the main API functions and their helper functions, and how all processes depend on JavaScript objects representing behavior instances in the document. Figure 9.12. The API procedure for processing server behaviors. User actions are indicated by text outside boxes.
We'll finish up this look at Server Behaviors with two workshops that will show you how to build behaviors with the SBB and beyond. For the projects and code written here, we'll be using the ASP/JavaScript server model. To develop and test them, you'll need access to an ASP server and a simple database.
note You can download some sample ASP and database files to play with from this book's companion web site, www.newriders.com (see Appendix F, "Contents of the Extensions Workshop Companion Web Site" for full details).
|