use SpringGraph component in a Cairngorm driven application
Nest Cairngorn class, representing Value Object pattern from SpringGraph package Item() class and add custom event extends CairngormEvent.
In this recipe we will use in same application Cairngorm ( http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm ) framework and sweet graph visualization component SpringGraph from Mark Shepherd (http://mark-shepherd.com/SpringGraph/).
Main idea of this recipe is to realize a class nested from SpringGraph package class Item() - base class representing atomic graph node and implementing IvalueObject interface from Cairngorn framework keep to whole MVC mechanism working. Object needed should work as a Value Object in Cairngorm structure from one side and be appropriate to build a SpringGraph based on it. So we should place this class to vo package, it should implement IvalueObject to match Cairngorm and it should extends Item() to build SpringGraph component.
Let's explore example class VOItem:
package vo
{
import com.adobe.cairngorm.vo.IValueObject;
import com.adobe.flex.extras.controls.springgraph.Item;
public class VOItem extends Item implements IValueObject
{
public function toString() : String
{
var s : String = "VOItem[id=";
s += id.toString();
s += " ]";
return s;
}
public function VOItem(data:XML):void{
super(data.attribute("id")[0]);
this.name = String(data.attribute("name")[0]);
}
}
}
The usual plactice is Value Object should have some unique id. Good news is Item() already has an id property – so we don't need to define it in our class, we nested it from Item(). Item() has “data” property of Object type so, to be honest, we can use it to store any data will be needed to transfer via Cairngorm. In this example for more obviousness let's consider the String id to be enough to build SpringGraph element (can be used for label text, for example).
Other improvement is Item() takes a String or null as constructor parameter – we used XML here (application used HTTPService with resultFormat set to HTTPService.RESULT_FORMAT_XML so naturally way is to use XML as constructor parameter for our class).
Let's imagine that in ModelLocator representing class we will need a setter like following:
public function set selectedVOItem(value:VOItem):void{
if(value != selectedItem){
selectedItem = value;
CairngormEventDispatcher.getInstance().dispatchEvent( new GetVOItemEvent( value.id ) );
}
}
When new VOItem is set as selectedItem some actions makes on GetVOItemEvent event dispatching through CairngormEventDispatcher. In Cairngorm framework we have construction like
CairngormEventDispatcher.getInstance().dispatchEvent( new CairngormEvent( CistomEvent.STATIC_CONSTANT_NAME ) );
So, to keep Cairngorm working we should add new GetVOItemEvent class extends CairngormEvent in event package. See following example:
package event
{
import com.adobe.cairngorm.control.CairngormEvent;
import flash.events.Event;
public class GetVOItemEvent extends CairngormEvent
{
public static var EVENT_GET_VOITEM : String = "getVOItem";
public var rootItemID : String;//let's imagin we should hold item-initiator index, in event code it's unique
/**
* Constructor.
*/
public function GetVOItemEvent(id:String)
{
this.rootItemID = id;
super( EVENT_GET_VOITEM);
}
/**
* Override the inherited clone() method, but don't return any state.
*/
override public function clone() : Event
{
return new GetVOItemEvent(this.rootItemID);
}
}
}
Now we can use Cairngorn mechanism to transfer the data (id) needed for StringGraph build. IcollectionView instance (Array(), ArrayCollection() etc.) of VOItem instances could be used as SpringGraph dataProvider. In itemRenderer, set for this dataProvider items we could use unique id to visual represent item.