I am new to Cairngorm and Flex (I'm All About Flash) so bear with me.
I am creating a component and I want it to use Cairngorm. I also want
to be sure that some events fired from the component are only heard
by the Cairngorm of the single component instance. Component
namespaces won't help because I use multiple instances of the same
event, I could probably filter by instance, but I am trying not to
hack (you'll probably see the irony after the next sentence...
If I use Cairngorm as is... ...and I utilize the Singleton
CairngormEventDispatcher, as soon as I put two instances of a
component on stage, the events from one instance cause the front
controller on the second instance to fire those events
Sadly this is probably operator error.
Though I am guessing that someone has had this problem before and
surely there is a best practice solution to this already, I thought I
would spend a little time to explore the issue further and find my
own solution.
I decided to try to handle this by using separate instances of the
CairngormEventDispatcher (MyViewHelper.dispatcher) for each instance
of my component. Then I could inject that dispatcher into
MyFrontController when it is instantiated so that it knows which
dispatcher to call.
Just when I think I am on to something, I had to override addCommand
so that it would add an event listener to that instance instead of
the Singleton CairngormEventDispatcher. This is great but it required
two modifications (minor, yet still modifications) to the Cairngorm
FrontController class in order to compile my creation. (see snippets
below) Basically I took two private things and made them protected.
Of course this might effect other pieces I have yet to build, but my
level of mastery coerces me into believing I have found a solid
answer. <snicker/> If those iteration two guys were really geniuses
they would have made these protected in the first place.
Ha, ha, ha, just kidding, (I crack myself up)
So I can either use my own FrontController impl or I can ask the
question: Is this a bug in Cairngorm? Or am I correct in beleiving I
must be doing something wrong...
Some code samples:
//
// myViewHelper Snippet:
//
public function MyViewHelper(view:Object)
{
dispatcher = new CairngormEventDispatcher();
controller = new MyFrontController(dispatcher);
...
}
//
// myFrontController Snippet:
//
public class MyFrontController extends FrontController
{
private var dispatcher:CairngormEventDispatcher;
public function MyFrontController(dispatcher:CairngormEventDispatcher)
{
super();
this.dispatcher = dispatcher
...
}
public override function addCommand( commandName : String,
commandRef : Class ) : void
{
if( commands[ commandName ] != null )
{
throw new Error( "Command already registered for " + commandName );
}
commands[ commandName ] = commandRef;
dispatcher.addEventListener( commandName, executeCommand );
}
}
//
// Cairngorm FrontController Mods
//
public class FrontController
{
// ST - 03/15/2007 : private var commands : Dictionary = new
Dictionary();
protected var commands : Dictionary = new Dictionary();
// ST - 03/15/2007 : private function executeCommand( event :
CairngormEvent ) : void
protected function executeCommand( event : CairngormEvent ) : void
...
}