OpenWGA 7.10 - OpenWGA Concepts and Features

Design and development » WebTML » Features » Actions » Creating controls to call actions

Calling from client side JavaScript

JavaScript running in the browser can also call WebTML actions. All WebTML pages which render <tml:htmlhead> can use the following built-in JavaScript functions to do this:

Both functions receive a single argument in form of a JavaScript object. This object contains all parameters for the action call as properties. The most important one, and mandatory on both functions, is "action":

WGA.action(

  { action: "<tml:action ref="myAction"/>" }

);

The action property takes an action link, generated by WebTML tag <tml:action>. Its ref attribute determines the action to call. Note the string delimiters around <tml:action>. They are necessary so the output of the WebTML tag will be evaluated as string  in JavaScript. This will get clear when seeing what is actually generated by <tml:action> and then sent to the browser:

WGA.action(

{ action: "##NULL##/4e453b3ffeae0f765dc35d415e99fd11/L4b_HWioetlYsODYD6TYQwzSgcNK2QCHLnJRQfH4ZC-_16e9ukM4j52jfaqEdgtF4EnQ0jys9Q-9PVCe6WI6iX7brAseVq17tTQ6zet2u9m6OUA9wfXrA999GWNBRcYRYNApcAE0pkngGbuJFqD5-RBVaj7Z3fsrO9qv17cLh4GMM7u-Ms7WIDjMi0L4cB1taSqUj_KhZd8Kihk-URvMVGyS2Pe4d6DxXNuONqLNAFd3aob90Msdysp_bme-ORCAKbq6QzFf3TM=" }

);

When using WGA.ajax.action() the <tml:action> tag must also specify attribute ajax="true".

WGA.ajax.action(

   { action: "<tml:action ref="myAction" ajax="true"/>" }

);

The parameter object of WGA.ajax.action() supports some properties to configure and tweak the AJAX behaviour. See the reference for details.

Specifying parameters from JavaScript

Optionally the param1...param5 attributes on <tml:action> can be used to determine WebTML action parameters for this action call.

WGA.action(

{ action: '<tml:action ref="myAction" param1="myParam"/>' }

);

However, when calling a WebTML action from clientside JavaScript this seldomly does what is really wanted. WebTML action parameters need to be determined on the serverside, like in this case is done by the <tml:action> tag. But most of the time when you call a WebTML action from clientside JavaScript you instead want to determine the values of its parameters also on the clientside.

There are two ways to provide clientside parameters to WebTML actions:

Use WebTML form fields as parameters

If you generate your action call inside a WebTML form then the action call will include the form data. This data then will be available to the WebTML form as "tmlform" object. So all data which your clientside JavaScript sets into the fields of the WebTML form will also be available to the action. If you already use a WebTML form this may be the easiest way to transport clientside parameters. Just add a hidden field to your form, and set its value just before you call the action. See the following example:

<tml:form id="theForm">


  <tml:input type="hidden" name="jsparam"/>


  <script>

    function callTheAction(theParam) {

      document.forms[0].elements['jsparam'].value = theParam;

      WGA.action( { action: '<tml:action ref="myAction'/>' } );

    }

  </script>


</tml:form>

The JavaScript function "callTheAction" first sets a value into the hidden input field "jsparam" of the HTML form (assuming that there is only one), then calls the action. When <tml:action> is rendered somewhere inside <tml:form> it automatically sends the form data along with the action call. If it is outside you can still tell it to send the form by using the form attribute.

On the serverside the action script can simply read the value of this field from the "tmlform" object:

var theParam = tmlform.jsparam;

Use URL parameters

Both functions WGA.action() and WGA.ajax.action() support adding URL parameters to the action call. You can do this by simply adding a property "params" to the function parameter object. The property itself should hold another JavaScript object whose properties become URL parameters on the action call:

WGA.ajax.action(

  {

    action: "<tml:action ref="myAction" ajax="true"/>",

    params: {

      key: theKey,

      title: "Hello World!",

      position: 5

    }

  }

);

The action call will have URL parameters "key", "title" and "position". The values of the properties will become the URL parameter values. Obviously you can pass on JavaScript variables here, like done with "theKey" in the example. You can also specify Non-String parameters, but be aware that they will automatically get converted to strings when they become URL parameters.

The called action can retrieve those URL parameters by using the method "getParameter()" on the this.request object in TMLScript:

var key = request.getParameter("key");

var title = request.getParameter("title");

var position = parseInt(request.getParameter("position"));

Note how we convert back the "position" parameter to a integer value.

Note that URL parameters are unsafe compared to WebTML action parameters. While the latter ones are encrypted and therefore unmodifiable as well as unreadable for the user URL parameters are not. They are always transmitted in cleartext and may easily be modified by the user. Do not depend the security of your app on the integrity of URL parameter values!