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

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

问题描述

我看到异步命令的好处(等待服务器响应...),但在我的flex应用程序,它创造了我更多的问题比任何。这是我想要的:



EVERY命令仅在返回上一个命令后才执行(结果或故障函数)



我想尽可能容易地做到这一点。通过GUI必须变得不负责任(可能是等待消息),而长命令正在执行(我可以在执行函数中显示等待消息,并删除它

解决方案

p>我通过扩展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;

/ *这个类是所有命令的基础。它允许用户设置回调
函数,以便成功完成和/或失败命令逻辑(即
一个WebService调用)。 * /
public class CairngormCommand implements ICommand
{
private var successHandler:Function;

private var failureHandler:Function;

public function execute(event:CairngormEvent):void
{
if(event是AbstractCairngormEvent)
{
var commandEvent:AbstractCairngormEvent =
事件作为AbstractCairngormEvent;

successHandler = commandEvent.successHandler;

failureHandler = commandEvent.failureHandler;
}
}

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

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

当必要的逻辑完成,或者有错误/失败时,我调用CairngormCommand基类中的相应函数。下面是一个例子:

  //这样的东西会在你的视图中:

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

myEvent.successHandler = callMyEventSuccessHandler;

myEvent.failureHandler = callMyEventFailureHandler;
}

私有函数callMyEventSuccessHandler():void {
Alert.show(SUCCESS !!!);
}

私有函数callMyEventFailureHandler():void {
Alert.show(FAILURE !!!);
}


//这是您的控制器中的事件:

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

//这是控制器中的命令:

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

... //调用WebServices
}
...

私有函数webServiceResultHandler():void
{
//处理结果
...

notifyCallerOfSuccess();
}

私有函数webServiceFaultHandler():void
{
//处理错误
...

notifyCallerOfFailure );
}
}
}

我使用这个EXCLUSIVELY Cairngorm应用程序。最好的部分是,如果你不需要成功和/或失败的回调,你可以把这些属性从事件实例化,并简单地派遣事件。



加载显示前需要显示的数据的示例

 <?xml version =1.0 encoding =utf-8?> 
< mx:Application xmlns:mx =http://www.adobe.com/2006/mxml
usePreloader =falseinitialize =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();
}

私人函数getSomeData():void
{
var event:GetSomeDataEvent = new GetSomeDataEvent();

event.successHandler = getSomeMoreData;

event.failureHandler = errorsHandler;

event.dispatch();
}

私人函数getSomeMoreData():void
{
var event:GetSomeMoreDataEvent = new GetSomeMoreDataEvent();

event.successHandler = getEvenMoreData;

event.failureHandler = errorHandler;

event.dispatch();
}

私人函数getEvenMoreData():void
{
var event:GetEvenMoreDataEvent = new GetEvenMoreDataEvent();

event.successHandler = notifyCallerOfSuccess;

event.failureHandler = errorHandler;

event.dispatch();
}

私人函数errorHandler():void
{
alert.show(error);
}
}
}


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?

解决方案

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

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

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