Flash_and_Flex_03_2009

ActionScript Development

calling methods on that service. The Delegate class has generally one argument when being instantiated, which is type IResponder, this provides any class using this Delegate to handle the results of the service in a nice manner.

• Identify – Service Delegate: • Holds server side methods with required arguments • Delegate class includes a responder to hold the result for command possessing

• Does not update the model • One Delegate per service that is declared in the Serivce Locator • Create FlexContactsDelegate.as , see (Listing 4) • Discuss – Service Delegate To use a Delegate you will generally create a variable inside of your Command that is of type FlexContactsDelegate, after the type you want to create a new instance of that Delegate. Now you will be able to call on a service that is declared inside of the delegate. Thus making a call to a server side service. See Commands below for more of a example. Cairngorm Events Cairngorm Events are just simple classes that dispatch to your application that something has happened, such as a user interaction of some sort or the completion of a process. When these events are dispatched via the View layer, the FrontController is the one that is listening for these events to happen, once an event has happened the controller then maps the event to the proper command for processing. Use cases of an application are usually the best events to create, ( AddToCart , Checkout , PaymentComplete , etc) Events must be unique, and always should include a static variable with the value set to the name of the event. Events that need to send data can just create arguments inside of the event along with public variables to hold the value of that data. Then when the processing of that event occurs, the processor has access to the public variables that are inside the event, holding the specific values of that event. • Idenfity – Cairngorm Event: • View layer can create and dispatch Cairngorm Events • Control layer can also create and dispatch Cairngorm Events • Control layer manages and process Cairngorm Events • Dispatching Cairngorm Events should be 1-way to the Control layer • CairngormEvents dispatch themselves to the Controller • Create GetContactsEvent.as , see (Listing 5). • Discuss Cairngorm Event. When creating events keep in mind that events and commands have a one to one (1 – 1) relationship. When you create an event, it is recommend creating a command that carries out the process of when that event happens. For instance youcreate aGetContactsEvent.as class be sure to create a GetContactsCommand.as class as well, when this event is dispatched the corresponding command will begin to process and carry out the functionality.

Listing 6. GetContactsCommand.as

package com.jonniespratley.flexcontacts.commands { import com.adobe.cairngorm.commands.ICommand; import com.adobe.cairngorm.control.CairngormEvent;

import com.jonniespratley.flexcontacts.business.FlexContactsDelegate; import com.jonniespratley.flexcontacts.events.GetContactsEvent; import com.jonniespratley.flexcontacts.model.FlexContactsModelLocator; import com.jonniespratley.flexcontacts.vo.*; import mx.collections.ArrayCollection; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; public class GetContactsCommand implements ICommand, IResponder { private var model:FlexContactsModelLocator = FlexContactsModelLocator.get Instance(); public function execute( event:CairngormEvent ) : void { trace( 'GetContactsCommand Executing' ); var evt:GetContactsEvent = event as GetContactsEvent; var delegate:FlexContactsDelegate = new FlexContactsDelegate( this ); delegate.getContacts(); } public function result( data:Object ) : void { import mx.controls.Alert; import mx.rpc.IResponder;

trace( 'GetContactsCommand Result Handling' ); var result:ResultEvent = data as ResultEvent; var contactArray:Array = data.result as Array var tempAC:ArrayCollection = new ArrayCollection(); for ( var o:Object in contactArray ) {

tempAC.addItem( new ContactVO( contactArray[ o ] ) );

} model.contactsCollection = tempAC;

} public function fault( fault:Object ):void {

var faultEvt:FaultEvent = fault as FaultEvent; Alert.show( faultEvt.fault.toString(), "Error" );

}

}

}

03/2009 (5)

56

Made with FlippingBook HTML5