5.8 Property calculate for Dynamic Computation
We saw an example of model property calculate in the shopping cart example in Section 4.3.3. The value of property calculate is an XPath expression that computes the value to be stored in the node to which the calculate property applies. Thus, when an instance node has model property calculate applied to it, it does not make sense to bind an XForms input control to that node. The XForms processor will automatically update the value of nodes having property calculate by evaluating the XPath expression provided after first updating the value of dependent nodes.
5.8.1 Examples of Property calculate
Contrast this with using a full-blown programming language, where the programmer would be free to create data models where calculating the value of a particular node might affect other unrelated parts of the form. By design, the dependency relationship set up among nodes in the XForms model by property calculate can be statically analyzed . The result of this analysis is a dependency graph that is used by the XForms processor in determining the order of automatic updates to the instance as the user interacts with the XForms application.
Note that property calculate sets up one-way dependencies among nodes in the instance; it is not meant to define a general constraint-solver. To see this, consider the temperature converter shown in Figure 5.10. Model property calculate on Fahrenheit relates this value to Centigrade . We bind control input to field Centigrade and control output to field Fahrenheit . Providing a value for Centigrade automatically updates field Fahrenheit , and the updated value is displayed by the output control that is bound to this field.
Figure 5.10 Property calculate to convert temperature.
< html xmlns ="http://www.w3.org/1999/xhtml" xmlns:xf ="http://www.w3.org/2002/xforms"> < head >< title >Temperature Converter</ title > < xf:model id ="temp"> < xf:instance xmlns =""> < temperature > < Centigrade >100</ Centigrade >< Fahrenheit /> </ temperature ></ xf:instance > < bind nodeset ="/temperature/Fahrenheit" calculate ="../Centigrade *9 div 5 +32"/> </ xf:model ></ head > < body > < xf:group ref ="/temperature" xmlns ="http://www.w3.org/2002/xforms"> < input ref ="Centigrade"> < label >Celsius</ label ></ input > < output ref ="Fahrenheit"> < label >Fahrenheit</ label ></ output > </ xf:group ></ body ></ html >
In XForms 1.0, binding an input control to field Fahrenheit and providing a new value for that field would not automatically update field Centigrade ; that is, the processor does not build the inverse relation between fields Fahrenheit and Centigrade . This is also why it does not make sense to bind XForms input controls to fields that have an associated calculate property.