0% found this document useful (0 votes)
38 views

Cairngorm Sample: Instance of Our Mainmodellocator

The document provides an overview of how a sample Cairngorm application works. It loads a default text message and allows the user to fetch a new message from the server by clicking a button. This is done through the MVC architecture of Cairngorm. When the button is clicked, an event is dispatched which calls a command that uses a delegate to retrieve the new message from the server and update the model, which then updates the view.

Uploaded by

JileniBouguima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Cairngorm Sample: Instance of Our Mainmodellocator

The document provides an overview of how a sample Cairngorm application works. It loads a default text message and allows the user to fetch a new message from the server by clicking a button. This is done through the MVC architecture of Cairngorm. When the button is clicked, an event is dispatched which calls a command that uses a delegate to retrieve the new message from the server and update the model, which then updates the view.

Uploaded by

JileniBouguima
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CairngormSample

Theapplica8onloadsanddisplaysadefaulttextmessagethatisheldintheMainModelLocator.
WhentheuserclicksGetMessagetheapplica8onfetchesanewtextmessagefromtheserver
(cfc)andupdatesthemessagepropertyinthemodel.Letsstepthroughit.
CairngormSample.mxml
Themainapplica8onle;youllno8ceacoupleofextrathingshappenhereoverandaboveany
othernoncairngormFlexapplica8on.FirstlytheframeworkprescribesthatwehaveaController;
thinkofthisasthebrainofyourapplica8on
<!-- instaniate the main controller -->
<control:MainController id="controller"/>

Controllersareonlyeverinstan8atedoncewithinthe<mx:Applica8on>tag,nowhereelse.Moreto
comeoncontrollerssoon.
Services(RemoteObjects)arealsoestablishedherelikeso
<!-- instaniate the services -->
<business:Services id="services"/>

Servicesareonlyeverinstan8atedoncewithinthe<mx:Applica8on>tag,nowhereelse.
Becausewewanttodisplayamessage(thatsheldinthemodel)wellneedtogetareferencetoan
instanceofourMainModelLocator
[Bindable]
private var mainModelLocator:MainModelLocator = MainModelLocator.getInstance();

Youllno8cethatthisisnotlikecrea8nganewmodellocatorobject.Thisisbecauseallmodel
locatorsaresingletonclasses,oneandonlyoneobjectofthetypeMainModelLocatorcaneverexist
intheen8reapplica8on.
Page1

MalcolmBarclaysCairngormSample

Nowthatwehaveareferencetothemodelwecanbindtoanypropertyinit
<mx:Label text="My Message Is: {mainModelLocator.message}"/>

IftheuserclicksGetMessage

Anewmessageisreturned.
Woah!Exci8ngstuhuh?Wheresthemagic?Star8ngwiththegetMessage()method
private function getMessage():void
{
// create a new 'get message event' and dispatch it
var getMessageEvent:GetMessageEvent = new GetMessageEvent();
CairngormEventDispatcher.getInstance().dispatchEvent( getMessageEvent );
}

AnewgetMessageEventofthetypeGetMessageEventwascreatedanddispatchedtothe
CairngormEventDispatcher.ThisnottoodissimilartootherFlexeventdispatchingyoumayhave
done(infactunderthehoodCairngormactuallyusestheFlexEventmodel).
Asanasidenoteonnamingconven8ons,allClassesshouldbenamedwithacapitalisedrstle`er
(i.eGetMessageEvent)andallClassInstancesshouldhavethesamename(ornearenoughto)with
alowercaserstle`er(i.egetMessageEvent).LetsexaminetheGetMessageEvent

Page2

MalcolmBarclaysCairngormSample

GetMessageEvent.as
Deningeventsispre`yeasyandisocenacopy&paste,rename,removeorcreateafewpublic
proper8esandyouredone.
AlleventsextendtheCairngormEventandneedtoimplementtwomethods;aconstructorandan
overrideoftheclone()func8oninheritedfromCairngormEvent.Youneverneedtochangethese
muchotherthanthenames.super()getscalledtoensurethatthenameoftheeventinques8onis
passedtotheconstructorofCairngormEvent.Youllneverneedtomesswiththis.
Whendeningeventsyouneedtoalsoneedtodeclaretheproper8esthatyouwanttotravelalong
withtheevent.Forexampleifyouareupda8ngacompanyyouwoulddeneapublicvartohold
thecompanyVO(i.etheupdatedcompanyvalueobject)sothatviewsdispatchingtheeventcanset
anupdatedcompanyobjectontheupdateCompanyEventandthendispatchit.
InthissampleGetMessageEventhasnopublicproper8es.
MainController.as
Turningoura`en8onbacktothecontroller;whentheapplica8onloadedthecontrollerwas
instan8ated.ItsherewherewemarryupEventsandtheCommandsthatwillexecutewhenan
eventisdispatched.
package net.mbarclay.cairngormsample.control
{

import com.adobe.cairngorm.control.FrontController;
import net.mbarclay.cairngormsample.event.GetMessageEvent;
import net.mbarclay.cairngormsample.command.GetMessageCommand;
public class MainController extends FrontController
{
public function MainController()
{
initialiseCommands();
}

public function initialiseCommands():void


{
addCommand( GetMessageEvent.GET_MESSAGE_EVENT,
GetMessageCommand );
}
}
}

AllcontrollersextendtheCairngormFrontController.UsingtheaddCommand()methodofthe
FrontControllerwearetellingtheframeworkwhenaneventofthetypeGetMessageEventis
dispatchedIwantyoutopassthat(execute)toGetMessageCommand.

GetMessageCommand.as
public function execute( event:CairngormEvent ): void
{
Page3

MalcolmBarclaysCairngormSample

var delegate : MessageDelegate = new MessageDelegate( this );


delegate.getMessage();
}

Whenaneventisdispatchedtheframeworkwillautoma8callycalltheexecute()methodinthe
commandclassforuspassingtheeventobjectin.
CommandsthenenlistthehelpofaDelegatetoperformcommunica8onwiththebackend
(whateverorwhereverthatmaybe).

Ali`lebitoftheoryCommandsexisttoperformanumberoftasks;howevertheyaremostly
concernedwiththefollowing:

Execu8nglogicforaneventthathasbeenpassedtoitbythecontrol(FrontController).The
commandinstan8atestheappropriatedelegateandexecutestheappropriatemethodon
thatdelegate.
Upda8ngthemodelupontheresultcomingbackfromthedelegate,thishappensinthe
resultmethodofthecommand;thedelegatecallsthiswhenaresponseisreceived(using
IResponder).

Commandskeepaclearsepara8onbetweentheuserinterface(theview)andtheunderlying
businesslogicoftheapplica8on.TheviewbywayofdispatchingaCairngormEventactuallyhasNO
ideawhatcommandwillevenexecuteasaconsequenceofdispatchingthatevent.
AveryimportantpointtonotehereisthattheCommandNEVEREVERstartsupda:ngorchanging
stuintheVIEWinitsresultmethod.TheCommandupdatestheMODELandtheVIEWisboundto
changesinthemodelandreactsaccordingly.CommandsknownothingabouttheView.AndViews
knownothingabouttheCommand.
Asidefromthesternwarningaboveitispossibletohavecommandsdirectlycallanymethodyou
caredirectlyonaview.Butassoonasyoudothisyouveboundtheimplementa8onofthat
commandtothatviewandintroducedadependency.Thereareotherandbe`erwaystoachievea
desiredreac8onintheviewuponresultuseaChangeWatcherinyourviewtowatchforchangesin
apar8cularpropertyinyourmodel.
Commandsimplementthefollowinginterfaces:

ICommand(partofcairngormframework)(required)
o execute()
IResponder(partofex)(op8onal)
o result()
o fault()

CommandsdonothavetoimplementtheIResponderinterface;thatistheydonthavetohavea
result&faultmethod.Butofcourseifyoureexpec8ngsomethingbackfromadelegateyouneed
toimplementIResponderinthecommand,nearlyallwillexpectsomethingback.
Otherlogicthatyoullndincommandclasseswillbepreparingdataforthemodel.Thishappensin
theresultmethod.Theoryover.
Page4

MalcolmBarclaysCairngormSample


MessageDelegate.as
Delegatesexisttoseparatetheunderlyingdatabase(oranyotherpersistencelayer)fromthe
Commandclasses,itsnotthejobofthecommandtocommunicatewiththedatabaseor
webservices.Delegatescanbesharedamongstanynumberofcommandsthataregoingaboutthe
taskofrespondingtodispatchedevents.
Onecouldchangetheen8rewayanapplica8onconnectedtothebackendandtheonlyclasses
youdneedtotouchwouldbethedelegates.
LookingattheMessageDelegate
private var responder : IResponder;
private var service : Object;
public function MessageDelegate( responder : IResponder )
{
this.service = ServiceLocator.getInstance().getRemoteObject( "messageService" );
this.responder = responder;
}

Backinthecommandclasswecreatedanewdelegate;whenthatdelegatewasinstan8atedits
constructor(methodnamethatmatchesexactlytothenameoftheclass)wasautoma8cally
executed.Twoimportantthingshappenhere:
One
UsingtheServiceLocatorwegetareferencetoourRemoteObjectcalledmessageService.The
serviceswereestablishedwhentheapplica8onloadedintheServices.mxml
<cairngorm:ServiceLocator
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="http://www.adobe.com/2006/cairngorm">
<mx:RemoteObject
id="messageService"
destination="ColdFusion"
source="net.mbarclay.cairngormsample.cfcs.MessageServices"
showBusyCursor="true"/>
</cairngorm:ServiceLocator>

ThisiswhereALLremoteobjectsareheldfortheen8reapplica8on.
Two
AresponderissetsothatwhenaresponseisreceivedfromtheCFCtheframeworkcanpassthis
backtothecommandthatcalledit.
GoingbacktoourGetMessageCommandclassacerestablishingadelegatethefollowingwascalled
delegate.getMessage();

ThisinturnexecutedthefollowingintheMessageDelegate

Page5

MalcolmBarclaysCairngormSample

public function getMessage():void


{
/**
* IF YOU HAVE SETUP COLDFUSION CORRECTLY UNCOMMENT THE LINES BELOW
* AND YOU'LL GET THE 'MESSAGE' RETURNING FROM THE CFC
*/
var call : Object = service.getMessage();
call.addResponder( responder );
}

Thecodeservice.getMessage();executesthegetMessagemethodontheCFC.
<cffunction name="getMessage" access="remote" returntype="String">
<cfreturn "I hear ya! From the CFC">
</cffunction>

Atextmessageisreturnedandaresponseisaddedtothecall.Fromheretheframeworktakes
overandreturnsthatresponsetotheresult()methodinourcommandclass.Movingbacktothe
commandclass
GetMessageCommand.as
public function result( event : Object ) : void
{
mainModelLocator.message = event.result as String;
}

ThemodelisthenupdatedwiththemessagereceivedbackfromtheCFC.Becausethelabeltextis
boundtothesamepropertyinthemodelitchangesautoma8cally.

Thecircleoflifeisnowcomplete.
Andofcourseduringallofthisanynumberofothereventscouldhavebeendispatchedand
a`endedto.Whenyoustartusingtheframeworkyoullquicklygainanapprecia8onforitstrengths,
asyourprojectsrapidlyincreaseincomplexityandfunc8onalityCairngormstartspayingdividends.

Page6

MalcolmBarclaysCairngormSample

You might also like