Flash_and_Flex_03_2009

ActionScript Development

Using your custom Cairngorm Event is really simple, so instance inside your view of the application if you created this GetContactsEvent.as file and you wanted to dispatch this event to your application, letting the application know that this event happened, you simply use the following.

This lets us have an array collection of strong typed objects inside our model for any part of the application to reference this. Doing a little more with commands • Create LoginCommand.as , see (Listing 7). • Discuss– LoginCommand.as With this command we are simply checking wither or not the values passed with the event

match what we are declaring in the execute() function of this command. The LoginEvent.as class has one argument which is a UserVO value object, when this event is dispatched it passes a user object holding username and password values. When the command receives the event it checks if the values of the UserVO.username and password match what is declared above.

private function getContacts():void { var evt:GetContactsEvent = new

GetContactsEvent();

Listing 9. LoginForm.mxml

evt.dispatch(); }

Commands The Command class then processes the event by running the Command class execute() method, which is an ICommand interface method. The event object may include additional data if required by the developer. The execute() method can update the central Model, as well as invoke a Service class which typically involves communication with a remote server. The IResponder interface, which is also implemented by the Command class, includes onResult and onFault methods to handle responses returned from the invoked remote service. Commands come in two flavors, one flavor is if your command implements IResponder then you must be calling a service inside of the delegate of some sort, then if you implement IResponder you must import the mx.rpc.IResponder , since the Cairngorm IResponder has been depreciated. You include two more functions inside of your command, result() and fault() these functions will handle the results from the server wither it be a success result or a fail result, you can also event dispatch another event from your command, such as when you get a fault from the call you can dispatch a FaultEvent passing in your parameters that are from the fault.

width="100%" height="100%" styleName="padding"

horizontalAlign="center" verticalAlign="middle" xmlns:mx="http://www.adobe.com/2006/mxml">

import com.jonniespratley.flexcontacts.vo.UserVO; import com.jonniespratley.flexcontacts.events.LoginEvent; private function checkLogin():void { var user:UserVO = new UserVO( txt_username.text, txt_ password.text ); var e_login:LoginEvent = new LoginEvent( user ); e_login.dispatch(); }

]]>

• Identify – Cairngorm Command: • Updates Model

• Calls on Delegate • One to One Event

• Create

GetContactsCommand.as ,

see

(Listing 6). • Discuss: GetContactsCommand.as

In this type of command you might notice something different, this command implements two interfaces, these interfaces are required when you command is going to be working with a remote service (RPC). In the result of this command we are taking the result object, and passing it to a instance of a new value object, then adding it to a temporary array collection before updating the model.

03/2009 (5)

58

Made with FlippingBook HTML5