Thursday, September 30, 2010

Cairngorm 3 Example

Publishing Cairngorm 3 Adobe tries to keep itself away from frameworks battle. Cairngorm 3 is an architecture which should be followed with one IOC ( Inversion Of Control) framework present in market. There are so many IOC frameworks available like Parsley, Swiz, RobotLegs and Spring In Actionscript are some of them. Again its a tough task to choose one IOC frameowrk as each one has its own advantages. One of the Advantage of Cairngorm 3 is it helps to write test driven projects.

Alongside of Cairngorm 3 Adobe published some libraries to use if someone thinks its necessary for the project. One may not use a single library and still follow Cairngorm 3. Some of the Libraries supports Parsley (IOC) framework. Yes Parsley is a mature framework (But what if some better frameworks will come in future, will Adobe support them all...oh, thats a different topic).

When I was trying to jump to Cairngorm 3, I found that Adobe docs are not clear enough to start. I didn't find much examples. Here I developed a simple example in Cairngorm 3 and Parsley 2.3.0. code is not well optimized. The original code was written by Joel Hooks to show a Parsley example. Here you can find more in the blog post. You can download the source code of my project here.

Hope this will help others to start Cairngorm 3.
Other examples you can find here.

Adobe Cairngorm 3 Simple Sample application explained.


Tuesday, September 7, 2010

Cairngorm 2 View Notification

Though Cairngorm 3 is out there by Adobe, many developers still using Cairngorm 2 (Model-View-Controller) framework for their projects. They find a common issue like View notification when a command updates data from any service. There are many ways discussed, to solve this problem. There are different situations which need to handle differently.

  1.  If we are not using objects which dispatches event on update (like ArrayCollection) and we want to inform the view after we got an update form server.
  2.  If we want to notify different views on data update and want to write our custom codes to update the display.

For the first case UniversalMind's CairngormExtension library(open source) provides a better solution. Which helps to update the view which invoked that event. It also gives more extensions to the Basic Cairngorm which is very helpful.
You can find the source code of the library here.

If you are not interested in whole library you can achieve this by creating custom event and custom command.
In the custom event you need to pass an extra argument as responder. That responder will hold the result and fault handlers which will be executed after data updates. The custom command holds the view responder and returns back the data to it. An example with  source code (java + flex) is available here.

We can achieve the same by creating a view object in Modellocator or by application and dispatch events like second case code. However it is not the best practice to do so.

For the second case we can dispatch a custom event by application and handle it from view.
Add the event listeners in creationcomplete method in the views which need update their display like:

protected function group1_creationCompleteHandler(event:FlexEvent):void
{
                // TODO Auto-generated method stub
                 FlexGlobals.topLevelApplication.addEventListener(HelloWorldEvent.SAY_HELLO, handleEvent);
}
           
private function handleEvent(event:HelloWorldEvent):void
{
                  Alert.show(event.msg, "Hurahh");
}


Dispatch the event from command class's result method by application. You can keep your data object in model locator or in event itself.

public function result(data:Object):void
{
            var event:HelloWorldEvent = new HelloWorldEvent(HelloWorldEvent.SAY_HELLO);
            event.msg = data.result;
            FlexGlobals.topLevelApplication.dispatchEvent(event);
  }