Flex Cairngorm – Interacting with a Command, through an Event

Cairngorm is a lightweight micro-architecture framework for Rich Internet Applications built in Flex or AIR, defining best-practices for a RIA. Read More

When a Cairngorm command requires parameters to call the required delegate, a common (bad) practice is to fetch parameters from View Components / Calculate or Query from inside the Cairngorm Command, and end up writing complicated / un-understandable code inside Command files, when a Cairngorm event is being dispatched from > 1 AS files.

But isn’t all this going against the very purpose of using the Cairngorm architecture?
Why not have parameters passed to a Cairngorm Event every time its dispatched, and stay away from mixing C with M and V in a MVC Framework?

This post explains how you pass parameters to a Cairngorm command at runtime, via a Cairngorm Event.

Our data is held in a ‘data’ object of an Event and is used to ‘send’ parameters / data to a Command.
We start off by initializing the data object in the constructor of the event.

public function MyEvent():void
{
data = new Object();
}

Lets now ‘add’ data into this data object when a event is dispatched.

var event:MyEvent= new MyEvent();
event.data.myData1 = “Value1”;
event.data.myData2 = “Value2”;
event.dispatch();

Our parameters are also assigned everytime the event is dispatched, eliminating the need for parameters to be fetched in a Cairngorm Command. The Command class just needs to be modified a bit to read these parameters.

public function execute(event:CairngormEvent):void
{
var event:MyEvent = event as MyEvent;
var delegate:MyDelegate = new MyDelegate();
delegate.fn(event.myData1 , event.myData2);
}

Thus, each instance of the Command uses parameters given to the corresponding event, allowing the Command class to do what it does best.

We may again face issues, trying to obtain the result of the command in the class that dispatched the event.
Instead of going by the straight forward way of calling required methods (using Application.application / FlexGlobals.topLevelApplication) on obtaining a result in the Command Class, we can clean up our code a little more here by specifying the callback function for the resultEvent while dispatching the event.

We just need to declare another variable of type ‘Function’ in the Event Class to achieve this.

public class MyEvent extends CairngormEvent
{
static public var EVENT_ID:String=”customEvent”;
public var callbackFunction:Function;

public function MyEvent()
{
super(EVENT_ID);
data = new Object();
callbackFunction = new Function();
}
}

Now this callbackFunction variable needs to be assigned with the value (read function) that needs to be called on the ResultEvent for the corresponding Command.

private function dispatchMyEvent():void
{
var event:MyEvent= new MyEvent();
event.data.myData1 = “Value1”;
event.data.myData2 = “Value2”;
event.callbackFunction = result_MyEvent;
event.dispatch();
}

public function result_MyEvent(event:ResultEvent):void
{
// Read event.result
}

Finally, the Command class needs to make sure event.callbackFunction is taken into consideration while the Responder Object is being created.

public function execute(event:CairngormEvent):void
{
var event:MyEvent = event as MyEvent;

var responder:Responder = new mx.rpc.Responder(event.callbackFunction, onFault_load); // Parameters : ResultEvent, FaultEvent
var delegate:MyDelegate = new MyDelegate();
delegate.fn(event.myData1 , event.myData2);
}

private function onFault_load(event:FaultEvent):void
{
trace(event.message.toString());
}

Simple as that. The Command class is now free of all the complicated code that needs to be written for obtaining parameters.
‘Alerts’ in the resultEvent of the Command may also be removed now.. šŸ™‚

— Update – 30/10/2010 —

Example :
Validate Email Address against the Mail Server
Desc : Demonstrates Interacting with a Command, through an Event while using a Cairngorm Framework. Connects with a SOAP webservice.

Feel free to post in any questions.

Flex - Validate Email Address against the Mail Server
Flex - Paginated Datagrid



16 Responses to “Flex Cairngorm – Interacting with a Command, through an Event”

  1. Imableadola

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian, iwspo.net

  2. Jester

    I’m a little noob of the Flex world, can you post a full example? tnx

  3. The Fellowship.co Christian Community would like to invite the blog owner and everyone reading this blog to join our Christian community. Our website is open to anyone who is interested in discussing anything related to Christianity. You can join our new forum here: http://www.fellowship.co

  4. you already using event dispatcher what the use of callBack function???… Cairngorm make’s things so ridiculous, just say simple things has simple solution… just make a simple Responder call then dispatch it to where you want it without using delegates, customize events,…. ….

  5. Well, firstly, the purpose of Cairngorm event, commands, and delegates is to facilitate seamless development across developers / designers by separating Data, Presentation, and Business Logic, as in any MVC framework. So, Yes, If you are the only one working on a project (and the only one to maintain), using Cairngorm is ‘Ridiculous’. I work on enterprise level apps, and to me, with an entire team working on the same project, an MVC framework, specifically Cairngorm, is a boon.

    Coming to the Callback Function and Responders, let me assume you know and use Cairngorm, and NEED a MVC framework. Now, If you are using Responders, how does your Cairngorm command find the correct ‘Responder’ Fn if the (Cairngorm) event is being dispatched under various circumstances !

  6. ThankYou Griff…WHAT A GUY.. it was just last week i showed my friends about the effects you use, they are all impressed.. What a Great demo, you are sooo inspiring.

    -Murk

  7. Eran Golan

    Hi,

    Thanks for this post, just what I needed.
    Keep up the good work,

    Eran

  8. hi, Iam struggling that geting values from command class to view, through xml file these values need to come can you give me example how can we get.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>