Calling server-side script

Now that we have seen how to use client-side scripts in UI actions, we can look at taking this a step further and using client- and server-side script in the same UI action.

This can be achieved by first calling client-side script in the UI action, which then calls the UI action in the code, and therefore runs the server-side script. This is a slightly strange concept to imagine at first, so let's see how it works.

First, we need to take a look at the line of code that calls the UI action from the client-side code:

gsftSubmit(null, g_form.getFormElement(), '<ui_action_name>');

The preceding line of code calls the UI action, but this time, it will run it on the server side. When using this technique, you must ensure that the Action name field of the UI action is the same as the name referenced in the script. The first argument of gsftSubmit is for a control, but seeing as we don't want to use this, we just pass null. This second argument is to get the form; in our case, we just want to get the current HTML form. The third argument is the action name, so this needs to be our UI action action name.

Next, let's look at the server side of the script:

if(typeof window == 'undefined')
serverSideCode();

function serverSideCode() {
//Run the server side UI Action code

}

The first part of this code is an if statement to check that we are running on the server side and not the client side anymore. This little piece of code also ensures that we do not receive browser errors. If the if statement evaluates to true, we then call a function to run our server-side code.

We can use the preceding example and call the onClick function in our script field:

function onClick() {
// Write script here to run when an UI Action is selected
gsftSubmit(null, g_form.getFormElement(), 'incident_ui_action');

}

if(typeof window == 'undefined')
serverSideCode();

function serverSideCode() {
//Run the server side code

}

This now gives us a UI action that is running client- and server-side code. We will look at some further examples later on in the chapter.

This type of script can be very useful, often being used on the client side to ensure certain fields are filled in or conditions are met before submission and the server-side code is run.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.190.167