Cairngorm 2.2 – Self Dispatching Events

Cairngorm 2.2, the latest version of the Flex micro-architecture, released on Friday, was a fairly minor release with just a few new features and minor fixes.

However, I thought it would be worthwhile for me to document the motivation behind some of the changes.

Today, I’ll give a quick overview of self-dispatching events, which were added to the 2.2 release.

With Cairngorm versions up to and including 2.1, to dispatch an event that would be handled by your controller, you had to do the something like this:

var loginEvent : LoginEvent = new LoginEvent( username, password );
CairngormEventDispatcher.getInstance().dispatchEvent( loginEvent );

If you had many events in your application, this became a pretty long-winded way of doing something as simple as dispatching an event.

With Cairngorm 2.2, we’ve added a helper method to CairngormEvent, which all your controller events should extend. Now, you can do the following:

var loginEvent : LoginEvent = new LoginEvent( username, password );

Or even:

new LoginEvent( username, password ).dispatch();

Its a matter of personal style on which of these two you prefer, but they both give much more clarity to your code and (more importantly for some) need less typing.

7 Responses to Cairngorm 2.2 – Self Dispatching Events

  1. Stefan Schmalhaus says:

    I directly jumped from Cairngorm 2.0 to 2.2 so I don’t know if this has already been in 2.1: When I looked at the modified Cairngorm shop I noticed that the Cairngorm events override the “clone()” method. What’s the purpose of this?

  2. Russ says:

    So do I still need to add the events in the controller?

  3. Hi Stefan,
    See here for a description of why clone() is needed:

  4. Hi Russ,
    Yes, this feature exists purely to make dispatching easier – you still need to add the commands to your controller.

  5. bokel says:

    Nice addition! Definitely easier and it also removes all the references to the CairngormEventDispatcher.

  6. Roman says:

    But how do the events know “where” to dispatch?
    Kind regards

  7. Bill Shirley says:

    My pre 2.2 copy had the dispatch() method implemented already (and i’ve been using it exclusively).
    the event doesn’t know where it’s being dispatched to
    the dispatch() method uses the common centralized event dispatcher
    commands register with the dispatcher to respond to events