使 cairngorm 命令始终同步工作

发布于 2024-08-06 13:56:56 字数 214 浏览 6 评论 0原文

我看到了异步命令的好处(等待服务器响应...),但在我的 Flex 应用程序中,它给我带来了更多的问题。这就是我想要的:

每个命令仅在前一个命令返回后执行(返回结果或错误函数)

并且我想尽可能轻松地执行此操作..顺便说一下,GUI 必须变得无响应(可能是等待消息)正在执行一个长命令(我可以在执行函数中显示等待消息,并在错误或结果函数中将其删除..)

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2024-08-13 13:56:56

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

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:

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 基类中调用适当的函数。这是一个示例:

// 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 应用程序中专门使用它。最好的部分是,如果您不需要成功和/或失败的回调,您可以将这些属性保留在事件实例化之外,然后简单地分派事件。

在加载显示之前加载显示所需的数据的示例:

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

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

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");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文