views:

244

answers:

1

I see the benefit of asynchronous commands (waiting for server responses...) but in my flex app it creates me more problem than anything. Here's what I want:

EVERY command executes only after the previous one returned (to result or fault function)

And I'd like to do this as easily as possible.. by the way the GUI must become irresponsive (maybe a wait message) while a long command is being executed (I could show the wait message in the execute function and remove it in the fault or result function..)

any Idea?

A: 

I accomplished this by extending the CairngormEvent Class and adding two properties:

package control.events
{
    import com.adobe.cairngorm.control.CairngormEvent;

    public class AbstractCairngormEvent extends CairngormEvent
    {
        public var successHandler:Function;

        public var failureHandler:Function;

        public function AbstractCairngormEvent(type:String)
        {
            super(type);
        }
    }
}

And creating a new Class as a base for all Commands, which implements ICommand:

package control.commands
{
    import com.adobe.cairngorm.commands.ICommand;
    import com.adobe.cairngorm.control.CairngormEvent;

    import control.events.AbstractCairngormEvent;

    /* This class is a base for all Commands. It allows the user to set callback 
        functions for successful completion and/or failure of the Command logic (i.e. 
        a WebService call). */
    public class CairngormCommand implements ICommand
    {
        private var successHandler:Function;

        private var failureHandler:Function;

        public function execute(event:CairngormEvent):void
        {
            if (event is AbstractCairngormEvent)
            {
                var commandEvent:AbstractCairngormEvent = 
                    event as AbstractCairngormEvent;

                successHandler = commandEvent.successHandler;

                failureHandler = commandEvent.failureHandler;
            }
        }

        public function notifyCallerOfSuccess():void
        {
            if (successHandler != null)
                successHandler.call(this);
        }

        public function notifyCallerOfFailure():void
        {
            if (failureHandler != null)
                failureHandler.call(this);
        }
    }
}

Then in each Command Class, when the necessary logic is complete, or there is an error/failure, I call the appropriate function in the CairngormCommand base Class. Here is an example:

// Something like this would be in your view:

private function callSomeWebService():void {
    var event:WebServiceEvent = new WebServiceEvent();

    myEvent.successHandler = callMyEventSuccessHandler; 

    myEvent.failureHandler = callMyEventFailureHandler; 
}

private function callMyEventSuccessHandler():void {
    Alert.show("SUCCESS!!!");
}

private function callMyEventFailureHandler():void {
    Alert.show("FAILURE!!!");
}


// Here is the Event in your Controller:

package control.events
{
    import control.events.AbstractCairngormEvent;

    public class WebServiceEvent extends AbstractCairngormEvent
    {
        public static var EVENT_ID:String = "webService";

        public function WebServiceEvent()
        {
            super(EVENT_ID);
        }
    }
}

// And here is the Command in your Controller:

package control.commands
{
    import com.adobe.cairngorm.control.CairngormEvent;

    import control.commands.CairngormCommand;
    import control.events.WebServiceEvent;

    public class WebServiceCommand extends CairngormCommand
    {
        override public function execute(event:CairngormEvent):void
        {
            super.execute(event);

            ... // Call WebServices
        }
        ...

        private function webServiceResultHandler():void
        {
            // Handle results
            ...

            notifyCallerOfSuccess();
        }

        private function webServiceFaultHandler():void
        {
            // Handle fault
            ...

            notifyCallerOfFailure();
        }
    }
}

I use this EXCLUSIVELY in my Cairngorm applications now. The best part is, if you don't need to have a callback for success and/or failure, you can just leave those properties out of the event instantiation and simply dispatch the event.

Example of loading data needed for display before the display is loaded:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    usePreloader="false" initialize="initializeHandler();">
    <mx:Script>
        <![CDATA[
            import control.events.InitializeApplicationEvent;

            private function initializeHandler():void
            {
                initializeApplication();
            }

            private function initializeApplication():void
            {
                var event:InitializeApplicationEvent = 
                    new InitializeApplicationEvent();

                event.successHandler = initializationSuccessHandler;

                event.dispatch();
            }

            private function initializationSuccessHandler():void
            {
                applicationContainer.visible = true;
            }
        ]]>
    </mx:Script>

    <control:Services xmlns:control="control.*" />

    <control:Controller xmlns:control="control.*" />

    <view:ApplicationContainer xmlns:view="view.*" id="applicationContainer" 
        width="100%" height="100%" visible="false" />
</mx:Application>

InitializeApplicationCommand (notice how you can chain the events and callers as many times as you'd like):

package control.commands
{
    import com.adobe.cairngorm.control.CairngormEvent;

    import control.events.GetEvenMoreDataEvent;
    import control.events.GetSomeDataEvent;
    import control.events.GetSomeMoreDataEvent;
    import control.events.InitializeApplicationEvent;

    public class InitializeApplicationCommand extends CairngormCommand
    {
        override public function execute(event:CairngormEvent):void
        {
            super.execute(event);

            getSomeData();
        }

        private function getSomeData():void
        {
            var event:GetSomeDataEvent = new GetSomeDataEvent();

            event.successHandler = getSomeMoreData;

            event.failureHandler = errorHandler;

            event.dispatch();
        }

        private function getSomeMoreData():void
        {
            var event:GetSomeMoreDataEvent = new GetSomeMoreDataEvent();

            event.successHandler = getEvenMoreData;

            event.failureHandler = errorHandler;

            event.dispatch();
        }

        private function getEvenMoreData():void
        {
            var event:GetEvenMoreDataEvent = new GetEvenMoreDataEvent();

            event.successHandler = notifyCallerOfSuccess;

            event.failureHandler = errorHandler;

            event.dispatch();
        }

        private function errorHandler():void
        {
            alert.show("error");
        }
    }
}
Eric Belair
FYI, I built this logic based on information I saw somewhere else on the internet - I forget where, or I'd give them some credit!!!
Eric Belair
ok.. so this way I have a function called at the end of the command.. but look at this situation: I have an event dispatched at the preinitialize event whose command loads my model. Then this data is used to create the interface. I have to stop the execution till the command returns or else the interface will keep on being build without the data! I want a way to send an event and then wait till the corresponding command has finished, can this be done?
luca
That is exactly how I use this functionality!!! I'll edit the answer above to show you...
Eric Belair