使 Cairngorm 命令始终同步工作 [英] make cairngorm commands ALWAYS work synchronously

查看:21
本文介绍了使 Cairngorm 命令始终同步工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了异步命令的好处(等待服务器响应...),但在我的 flex 应用程序中,它给我带来的问题比任何事情都多.这是我想要的:

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)

而且我想尽可能轻松地做到这一点..顺便说一下,在执行长命令时,GUI 必须变得无响应(可能是等待消息)(我可以在执行函数中显示等待消息并在错误或结果函数中删除它..)

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..)

有什么想法吗?

推荐答案

我通过扩展 CairngormEvent 类并添加两个属性来实现这一点:

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);
        }
    }
}

并创建一个新类作为所有命令的基础,它实现了 ICommand:

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);
        }
    }
}

然后在每个命令类中,当必要的逻辑完成,或者出现错误/失败时,我调用 CairngormCommand 基类中的相应函数.下面是一个例子:

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();
        }
    }
}

我现在只在我的 Cairngorm 应用程序中使用它.最好的部分是,如果您不需要成功和/或失败的回调,您可以将这些属性排除在事件实例化之外并简单地调度事件.

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(注意您可以根据需要多次链接事件和调用者):

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");
        }
    }
}

这篇关于使 Cairngorm 命令始终同步工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆