Flash_and_Flex_03_2009

Flex Application Architecture

and where ever you go ( view ) you are going to have access to that same amount ( data ) that is inside your bank ( ModelLocator ).

Listing 4. GetContactsEvent.as

package com.jonniespratley.flexcontacts.business {

• Identify Model Locator:

import com.adobe.cairngorm.business.ServiceLocator; import com.jonniespratley.flexcontacts.vo.ContactVO;

• Central data bank for a application • Singleton means that only one instance of this class should exist • Does not contain any business logic • Supports data binding • Updated by the Command • Accessed by the View • Create FlexContactsModelLocator.as , see (Listing 2) • Discuss – ModelLocator To access data that is stored inside of the model you first create a Bindable private variable inside of your view or any class that needs to access the data, set the variable type to the name of your ModelLocator class ( Flex ContactsModelLocator.as ). Then you set that variable equal (=) to Fl exContactsModelLocator.getInstance() , creating an instance of your model in which allows access to any part of that data bank. Service Locator The Cairngorm Service Locator provides a clean and effective way of defining multiple server side sources in a single component; you can define any type of (RPC) remote procedure calls such as HTTP Services, Web Services (WSDL), Remoting Services (AMF), etc. The Service Locator is not created in the form of a class, but as a component based off of the Cairngorm Service Locator package. Creating this is just as simple as creating a new component, but instead of setting the based on to a VBox or a Canvas you set it to ServiceLocator, finishing it off by declaring the xml namespace to com.adobe.cairngorm.b usiness.* . • Identify – Service Locator: • Only one per applcication • Based off a singleton class • Central area for all server side destinations • Called by the Delegate • Create Services.mxml , see (Listing 3) • Discuss – Service Locator: Creating a new component that is based on the ServiceLocator will look like above then inside of that component you simply just place all of your services, be sure to give them the proper id for referencing them later on. The Delegate The Delegate in its simplest form is holder of the services in which you want to invoke on the server side. Delegates use the ServiceLocator for locating the service then

import mx.rpc.AsyncToken; import mx.rpc.IResponder; public class FlexContactsDelegate {

private var responder:IResponder; private var service:Object; public function FlexContactsDelegate( responder:IResponder ) {

this .service = ServiceLocator.getInstance().getRemoteObject( "AmfphpService" ); this .responder = responder;

trace( 'Locating Service' ); } public function getContacts():void {

var token:AsyncToken = service.getAllContacts(); token.addResponder( responder );

trace( 'Calling getContacts()' );

}

}

}

Listing 5. GetContactsEvent.as

package com.jonniespratley.flexcontacts.events {

import com.adobe.cairngorm.control.CairngormEvent; import flash.events.Event; public final class GetContactsEvent extends CairngormEvent { public static const GET_CONTACTS_EVENT:String = "GET_CONTACTS_EVENT"; public function GetContactsEvent() { super ( GET_CONTACTS_EVENT );

trace( 'GetContactsEvent Dispatching' );

} override public function clone():Event { return new GetContactsEvent(); }

}

}

03/2009 (5)

55

Made with FlippingBook HTML5