Home > Flex > Cairngorm – Chain Events

Cairngorm – Chain Events

August 7th, 2008

Found an Example of Chain Events at http://www.cairngormdocs.org/bjorn_schultheiss/eventChaining/EventChainExample.html (right click and check source).

So I put the code to an default Cairngorm Structure that you can download here. Just make sure that you have the Cairngorm.swc package in libs.

This is what files that exist.

cairngormdefault

Important to think of:
1.) All events extends ChainEvent

public class TestAEvent extends ChainEvent

2.) All commands extends SequenceCommand

public class TestACommand extends SequenceCommand implements ICommand

3.) All commands take the nextEvent and execute when done.

override public function execute(event:CairngormEvent):void
{
    nextEvent = ChainEvent(event).nextChainedEvent;
    // Do Command Action
    this.executeNextCommand();
}

That’s is…
Here is an example of executing a couple of commands.

var beginChain : ChainEvent = EventChainFactory.chainEvents([
    new UpdateViewEvent(ModelData.VIEW_LOADING),
    new CheckSoftwareVersionEvent(),
    new GetLocalSettingsEvent(),
    new GlobalApplicationEvent(),
    new GetFeedListEvent(),
    new UpdatePostStatusEvent(),
    new UpdateViewEvent(ModelData.VIEW_POSTS)
    ]);
cgDispatcher.dispatchEvent( beginChain );

————–
UPDATE: 2008-02-21
I’ve added a couple of changes for the demo files.
E.g. when creating an event it’s possible to send custom data
new TestAEvent( “CustomValue” )
and it’s just as easy to extract it in the command

var customEventValue:* = event.data;

You can add any data to the next event in the chain from the command, maybe to pass forward the data or something new. Here is an example.

nextEvent.data = customEventValue;

You can as well runtime change the next event from a command, here is an example.

nextEvent = new TestCEvent();

Flex , ,

  1. Irina Huzum
    February 20th, 2009 at 10:53 | #1

    Thank you for approaching the problem of chaining events. It brought me a step closer to what I’m looking for in the application I’m now developing.

    The missing step, however, would be a mechanism of passing data from one event to another.

    In your example, TestAEvent and TestBEvent do not carry any data along. Did you think of improving the EventChainFactory to enable it to:
    - pass data through the chain of events and
    - dispatch an event or another, depending on the result of the command executed by triggering the former event? Kind of like building a decision tree: execute A; if A was successful, then execute B, if not, then execute C.

    I’d be happy to hear that this was already implemented by somebody :)

  2. admin
    February 21st, 2009 at 23:12 | #2

    Hi Irina, glad you like it =)
    I’ve update the post with some hints of how to add custom data to event from the command. You can as well change the next event runtime from the commands if you want to use it in a Sequence Pattern.

    I’ve updated the download zip with the example changes.
    Hope it will helps you. Cheers

  3. Irina Huzum
    February 25th, 2009 at 14:48 | #3

    Hi Fredrick,

    Thank you so much for taking the time to update your example.
    I’m even one step closer to what will truly work for my application.

    Best,
    Irina

  1. No trackbacks yet.