Andrej Tozon's blog

In the Attic

NAVIGATION - SEARCH

Atlas EuroCalculator

This is more of a local thing, but Slovenia is getting ready to adopt the Euro currency and I thought I could come up with the SIT to EUR currency calculator, wrapping it in a useful declarative Atlas sample. And here it is - copy the following code into the Atlas-enabled web page:

<div>
   <input type="text" id="inputBox" size="5" class="input" value="0"/> SIT
   <input id="calculateButton" type="button" value="SIT -> EUR" />
   <span id="resultLabel">0</span> EUR
   
<span id="euroValidator" style="color:red"> *</span>
</
div>
<
script type="text/javascript">
   function CalculateEUR(sender, eventArgs)
   {
      
var value = eventArgs.get_value();
      
var newValue = 0;
      
if (value > 0)
         newValue = parseInt(value / 2.3964, 10) / 100;
      
eventArgs.set_value(newValue);
   }
</script>
<
script type="text/xml-script">
   
<page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
      <components>
         <textBox id=
"inputBox">
            <validators>
               <requiredFieldValidator errorMessage=
"Vpiši številko!" />
               <typeValidator type=
"Number" errorMessage="Vpiši številko!" />
               <rangeValidator lowerBound=
"0" upperBound="100000000" errorMessage="Številka mora biti večja od 0!" />
            </validators>
         </textBox>
         <label id=
"resultLabel">
            <bindings>
               <binding id=
"calculator" dataContext="inputBox" dataPath="text" property="text" transform="CalculateEUR" direction="Out" automatic="false" />
            </bindings>
         </label>
         <validationErrorLabel id=
"euroValidator" visibilityMode="Hide" associatedControl="inputBox" />
         <button id=
"calculateButton">
            <click>
               <invokeMethod target=
"calculator" method="evaluateIn" />
            </click>
         </button>
      </components>
   </page>
</script>

The code is, thanks to Atlas, pretty straightforward, so I don't think I need to explain it here. If you have some questions, comments or improvements to the above code, feel free to post them here.

Disclaimer: the above sample is for illustrative purposes only. Don't use it to perform any real-world calculations, since [the above javascript funcion] hasn't at all been tested for accuracy of the SIT -> EUR conversion.
Actually, the included javascript function does a poor job in calculating the accurate result. It probably would be best to create a web service, which would return the correct result and bind the calculator to it.

[Update: The code was actually calculating EUR -> SIT, not vice versa. Fixed.]