13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Handling errors<br />

function showWarningError(error:WarningError):void<br />

{<br />

var message:String = error.message + "\n\n" + "Do you want to exit this application?";<br />

showButtons();<br />

var title:String = error.getTitle();<br />

statusText.text = message;<br />

}<br />

Wh<strong>en</strong> the user clicks either the Yes or No button, the closeHandler() method is called. The following excerpt shows<br />

the closeHandler() method:<br />

function closeHandler(ev<strong>en</strong>t:CloseEv<strong>en</strong>t):void<br />

{<br />

switch (ev<strong>en</strong>t.detail)<br />

{<br />

case yesButton:<br />

showFatalError(new FatalError(9999));<br />

break;<br />

case noButton:<br />

statusText.text = "";<br />

hideButtons();<br />

break;<br />

}<br />

}<br />

If the user chooses to cancel the script by clicking Yes, a FatalError is thrown, causing the application to terminate.<br />

Building a custom validator<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The custom Validator class contains a single method, validateEmployeeXML(). The validateEmployeeXML()<br />

method takes a single argum<strong>en</strong>t, employee, which is the XML packet that you want to validate. The<br />

validateEmployeeXML() method is as follows:<br />

public static function validateEmployeeXML(employee:XML):void<br />

{<br />

// checks for the integrity of items in the XML<br />

if (employee.costC<strong>en</strong>ter.l<strong>en</strong>gth() < 1)<br />

{<br />

throw new FatalError(9000);<br />

}<br />

if (employee.costC<strong>en</strong>ter.l<strong>en</strong>gth() > 1)<br />

{<br />

throw new WarningError(9001);<br />

}<br />

if (employee.ssn.l<strong>en</strong>gth() != 1)<br />

{<br />

throw new FatalError(9002);<br />

}<br />

}<br />

To be validated, an employee must belong to one (and only one) cost c<strong>en</strong>ter. If the employee doesn’t belong to any cost<br />

c<strong>en</strong>ters, the method throws a FatalError, which bubbles up to the validateData() method in the main application<br />

file. If the employee belongs to more than one cost c<strong>en</strong>ter, a WarningError is thrown. The final check in the XML<br />

validator is that the user has exactly one social security number defined (the ssn node in the XML packet). If there is<br />

not exactly one ssn node, a FatalError error is thrown.<br />

Last updated 6/6/2012<br />

72

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!