6. Write the applyServerBehavior() function
Key Concepts
|
Gather input from the user interface |
|
Using the SSEdits class, call add() and insert() methods |
First, add another global constant containing the run-time code to be added to the page.Where the recordset name and the URL will go, put unique placeholders @@rs@@ for recordset and @@url@@ for the URL. Also, be sure to use backslashes to escape any quotes around these placeholders.
var CHUNK1 = "<% if (@@rs@@.EOF) Response.Redirect(\"@@url@@\"); %>";
Now you are ready to write the applyServerBehavior() function. Start by getting the recordset and the URL from the user interface. The recordset selection is easily retrieved using the get() method of the ListControl class. For the URL, get the value of the object:
var rs = LIST_RS.getValue();
var url = TEXT_URL.value;
The URL is required. If the user forgot to enter it, an error message should appear and the dialog box will not close. To achieve this behavior, the applyServerBehavior() function can return a string and Dreamweaver will display it as an error message and prevent the dialog from closing. Initialize an error string called errStr , and set it only if the URL is empty. The complete function so far should look like this:
function applyServerBehavior(ssRec) {
var rs = LIST_RS.getValue();
var url = TEXT_URL.value;
var errStr = "";
if (!url) {
errStr = "Please enter a URL.";
}
if (!errStr) {
//insert the runtime code
}
return errStr;
}
Try it out. If you don't enter a URL, the dialog should display an error message.
If there is a URL, you can insert the run-time code on the page. Get the run-time chunk ( CHUNK1 ) and copy it to a local variable. Next, use regular expressions to replace the placeholders with the actual values for the recordset and URL:
chunk = CHUNK1;
chunk = chunk.replace(/@@rs@@/g,rs);
chunk = chunk.replace(/@@url@@/g,url);
Now chunk contains the exact run-time string to be inserted. Inserting code in the user's document is made simple by using the SSEdits class (refer to ssClasses.js for documentation). First, create an instance of the class and call it editList :
editList = new SSEdits();
For each chunk to be added, use the add() method (in this example, there's only one chunk to add). The first parameter of add() is the string to insert (use the "chunk" variable from above). The second parameter will be handled in step 9; for now just use a null call (a space enclosed by double quotation marks ) as a placeholder. The third parameter is the insertion weight. All ASP tags above the HTML tag have numerical weights from 0 to 99 to keep them ordered. Recordsets have a weight of 50 by convention. To ensure the run-time code is inserted below the recordset but above other code, give it a weight of 51 .
editList.add(chunk,"",51);
Once all the chunks have been added (there's only one chunk in this example), call the insert() method:
Here's the complete applyServerBehavior() function:
function applyServerBehavior(ssRec) {
var rs = LIST_RS.getValue();
var url = TEXT_URL.value;
var errStr = "";
if (!url) {
errStr = "Please enter a URL.";
}
if (!errStr) {
chunk = CHUNK1;
chunk = chunk.replace(/@@rs@@/g,rs);
chunk = chunk.replace(/@@url@@/g,url);
editList = new SSEdits();
editList.add(chunk,null,51);
editList.insert();
}
return errStr;
}
Your server behavior file should now look like the sample file Step06_RedirectIfEmpty.htm.
Restart Dreamweaver and open the test file. Open the server behavior window. Click the plus (+) menu, select Redirect If Empty. Type something in URL textfield and click OK. Now examine the HTML source code to ensure the run-time code was added.
|