org.apache.cocoon.forms.formmodel.algorithms
Class JavaScriptBuilder

java.lang.Object
  extended byorg.apache.cocoon.forms.formmodel.algorithms.JavaScriptBuilder
All Implemented Interfaces:
CalculatedFieldAlgorithmBuilder

public class JavaScriptBuilder
extends Object
implements CalculatedFieldAlgorithmBuilder

Javascript based calculated field algorithm builder.

With this algorithm the user can implement it's own algorithm directly in javascript and directly inside the form definition.

The syntax is as follows:

   <fd:value type="javascript" triggers="items,price,discount">
     var price = parent.lookupWidget('price').getValue();
     var items = parent.lookupWidget('items').getValue();
     var discount = parent.lookupWidget('discount').getValue();

     if (discount == 'completelyfree') return 0;
     var total = price * items;
     if (discount == 'halfprice') return total / 2;
       return total;
   </fd:value>
 

From inside the javascript function the following objects are accessible:

form
The form object.
parent
The parent widget. This is very useful in repeaters, since the parent is the repeater row.
cocoon and other FOM objects
This are accessible only when flowscript is in use (see bug COCOON-1804)

As you can see, the function must return the calculated value, and not set this directly in the widget. This way the value can be converted correctly if needed.

Version:
$Id: JavaScriptBuilder.html 1304280 2012-03-23 11:18:01Z ilgrosso $

Field Summary
 
Fields inherited from interface org.apache.cocoon.forms.formmodel.CalculatedFieldAlgorithmBuilder
ROLE
 
Constructor Summary
JavaScriptBuilder()
           
 
Method Summary
 CalculatedFieldAlgorithm build(Element algorithmElement)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaScriptBuilder

public JavaScriptBuilder()
Method Detail

build

public CalculatedFieldAlgorithm build(Element algorithmElement)
                               throws Exception
Specified by:
build in interface CalculatedFieldAlgorithmBuilder
Throws:
Exception


Copyright 1999-2008 The Apache Software Foundation. All Rights Reserved.