Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

cairngorm command class structure

Hi guys
I am new to cairngorm, so had a simple question.

My project structure is i have summary screen which displays a ArrayCollection.
When user clicks on an element in a ArrayCollection it goes to detail screen. user clicks another button in detail screen.

My question is i want to have only one single Command class for summary screen and detail screen, is that possible?

something like

public class Mycommand impliments Icommand,Iresponder{
public function execute(event:CairngormEvent):void {
var summaryEvent:SummaryEvent = sumevent as SummaryEvent;
var detailEvent:DetailEvent = detevent as DetailEvent;

 }

result()
{
...
}

fault()
{
...
}
}
similarly i want to have only one delegate class for both SummaryEvent and  DetailEvent
will that be possible? I want to avoid multiple command class for different functionality.
and want to put everything in one command class.

thanks
ASKER CERTIFIED SOLUTION
Avatar of dgofman
dgofman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

Thanks.. Can you give me a simple code example.
Avatar of Jay Roy

ASKER

hi can you please show how this can be done in my case...I tried but couldnt impliment it.

how can i have multiple executes functions in a sigle command class? Or do i have multiple events inside a single execute function, not sure....

public class Mycommand impliments Icommand,Iresponder{
public function execute(event:CairngormEvent):void {
var summaryEvent:SummaryEvent = sumevent as SummaryEvent;
var detailEvent:DetailEvent = detevent as DetailEvent;

 }

result()
{
...
}

fault()
{
...
}
}

any help will be appreciated..thanks
What cairngorm  version are you using?
Avatar of Jay Roy

ASKER

Version 2.2
Ok!!, Fine I will help you with the design, but what I can do in one MXML file I have to split at least 5 AS classes. Answer on your question you will find on the end

1) You have to create a ServiceLocator class first

<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator 
	xmlns:fx="http://ns.adobe.com/mxml/2009" 
	xmlns:s="library://ns.adobe.com/flex/spark" 
	xmlns:mx="library://ns.adobe.com/flex/mx"
	xmlns:cairngorm="com.adobe.cairngorm.business.*">

	<fx:Declarations>
		<mx:WebService id="webService" wsdl="http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl" useProxy="false"/>
	</fx:Declarations>
</cairngorm:ServiceLocator>

Open in new window


2) We may need to create our custom Event class

package
{
	import com.adobe.cairngorm.control.CairngormEvent;

	public class ZipCodeEvent extends CairngormEvent
	{
		public var zipCodeList:Object;

		public function ZipCodeEvent(zipCodeList:Object) 
		{
			super(MyFrontController.GET_ZIPCODE_EVENT);
			this.zipCodeList = zipCodeList;
		}
	}
}

Open in new window


3) The Delegate class accessing services implemented in ServiceLocator by name

package
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.business.ServiceLocator;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.mxml.Operation;
	import mx.rpc.soap.mxml.WebService;

	public class ZipCodeDelegate
	{
		private var responder:Responder;
		private var service:WebService;
		
		public function ZipCodeDelegate(responder:Responder)
		{
			this.service = ServiceLocator.getInstance().getWebService("webService") as WebService;
			this.responder = responder;
		}
		
		public function LatLonListZipCode(zipCodeList:Object): void
		{
			var o:Operation = service["LatLonListZipCode"];
			o.resultFormat = "e4x";
			o.addEventListener(ResultEvent.RESULT, responder.onResult);
			o.addEventListener(FaultEvent.FAULT, responder.onFault);
			o.arguments = zipCodeList;
			o.send();
		}
	}
}

Open in new window


4) Command class listening responces from Delegate by passing self reference

package
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;

	public class ZipCodeCommand implements Command, Responder
	{
		public function execute(event:CairngormEvent):void{
			var delegate:ZipCodeDelegate = new ZipCodeDelegate(this);
			delegate.LatLonListZipCode(ZipCodeEvent(event).zipCodeList);
		}

		public function onResult(event:*=null):void{
			MyModelLocator.instance.zipCodeResult = event.result;
		}

		public function onFault(event:*=null):void{
			trace(event);
		}
	}
}

Open in new window


5) In Command:onResult  hander I implemented aissgment to my Model class, that reference will bing in our main MXML file and you can bind in any other classes where are u expecting to read a result

package
{
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class MyModelLocator extends EventDispatcher
	{
		private static var modelLocator:MyModelLocator;

		private var _zipCodeResult:Object;

		[Bindable(event="zipCodeChange")]
		public function get zipCodeResult():Object{
			return _zipCodeResult;
		}
		public function set zipCodeResult(value:Object):void{
			_zipCodeResult = value;
			dispatchEvent(new Event("zipCodeChange"));
		}

		public static function get instance():MyModelLocator {
			if (modelLocator == null)
				modelLocator = new MyModelLocator();
			return modelLocator;
		}
	}
}

Open in new window


6) And last class is Controller what responcible execute all these classes

package
{
	import com.adobe.cairngorm.control.FrontController;

	public class MyFrontController extends FrontController
	{
		public static const GET_ZIPCODE_EVENT:String = "zipCodeEvent";

		public function MyFrontController()
		{
			addCommand(GET_ZIPCODE_EVENT, ZipCodeCommand);
		}
	}
}

Open in new window


7) Here is my Main.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   xmlns:cairngorm="com.adobe.cairngorm.business.*" 
			   width="100%" height="100%" xmlns:local="*">

	<fx:Script>
		<![CDATA[
			import com.adobe.cairngorm.control.CairngormEventDispatcher;
			
			[Bindable]
			private var latitude:String;
			[Bindable]
			private var longitude:String;

			[Bindable]
			private var model:MyModelLocator = MyModelLocator.instance;

			public function onClick():void{
				var event:ZipCodeEvent = new ZipCodeEvent({zipCodeList:zipcode.text});
				CairngormEventDispatcher.getInstance().dispatchEvent(event);
			}

			private function set zipCodeResult(result:Object):void{
				if(result is XMLList && result.listLatLonOut.length()){
					var xml:XML = new XML(unescape(result.listLatLonOut));
					var latLon:String = xml.latLonList[0];
					if(latLon != null && latLon.indexOf(",") != -1){
						var params:Array = latLon.split(",");
						latitude = params[0];
						longitude = params[1];
						log.text = ("latitude=" + latitude + ", longitude=" + longitude);
					}
				}
			}
		]]>
		
	</fx:Script>
	<fx:Binding source="model.zipCodeResult" destination="zipCodeResult"/>
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<mx:Label text="Enter zip code:"/>
	<mx:TextInput id="zipcode" text="94040" maxChars="5"/>
	<mx:Label text="Number of days:"/>
	<mx:TextInput id="numDays" text="5" maxChars="2"/>
	<mx:Button label="Submit" click="onClick()"/>
	<mx:TextArea id="log" width="100%" height="100%"/>
	<fx:Declarations>
		<local:Services id="services"/>
		<local:MyFrontController/>
	</fx:Declarations>
</s:Application> 

Open in new window


So, I have main class what is firing ZipCodeEvent to cotroller, controller class creating command, command creates delegate, delagate calls webservices and assigng result to model. As I mention in step 5 I binding delegate result in my  main class to display a result. You can do the same for other classes
Avatar of Jay Roy

ASKER

thanks for help. I really appreciate it.

any help with my next question will be greatly appreciated
https://www.experts-exchange.com/questions/27036956/hide-unhide-a-view-in-a-viewstack.html