?
Solved

cairngorm command class structure

Posted on 2011-05-08
7
Medium Priority
?
391 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:royjayd
  • 4
  • 3
7 Comments
 
LVL 29

Accepted Solution

by:
dgofman earned 1600 total points
ID: 35717571
You can you simgle command and add multiple event listeners to handle responce in one or many function handlers
0
 

Author Comment

by:royjayd
ID: 35717739
Thanks.. Can you give me a simple code example.
0
 

Author Comment

by:royjayd
ID: 35742261
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:dgofman
ID: 35744431
What cairngorm  version are you using?
0
 

Author Comment

by:royjayd
ID: 35746164
Version 2.2
0
 
LVL 29

Expert Comment

by:dgofman
ID: 35751388
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
0
 

Author Comment

by:royjayd
ID: 35755597
thanks for help. I really appreciate it.

any help with my next question will be greatly appreciated
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Flex/Q_27036956.html
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question