如何在httpservice结果事件中调度自定义事件

时间:2011-12-08 15:41:22

标签: actionscript-3 flex air

在我的AIR应用程序中,我尝试将一个自定义事件从类调度到主窗口。 该类用于调用httpservice。我的目标是在发送httpservice结果时发送自定义窗口。

package fr.inter.DataProvider
{
    import flash.events.Event;
    import flash.events.EventDispatcher;

    import fr.inter.config.urlManager;
    import fr.kapit.introspection.components.DisplayListComponent;

    import mx.collections.XMLListCollection;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    [Event(name="evtPatSelect", type="flash.events.Event")]

    public class sPatient
    {

        private var _phppaIndex:String;
        private var _phppaNomU:String;
        private var _phppaPrenom:String;
        private var phpSearchPatNom:HTTPService;


        public function sPatient()
        {

        }


        public function sPhpSearchPat(p:Object):void
        {

            phpSearchPatNom = new HTTPService();
            phpSearchPatNom.method="POST";
            phpSearchPatNom.resultFormat = "e4x";
            phpSearchPatNom.addEventListener(ResultEvent.RESULT,resultListePatient);
            phpSearchPatNom.addEventListener(FaultEvent.FAULT,serviceFault);
            var urlPhp:urlManager=new urlManager();
            phpSearchPatNom.url = urlPhp.urlService() + "20SearchNom.php";
            phpSearchPatNom.send(p);
        }


        private function resultListePatient( event:ResultEvent ):void
        {

            var xmlList:XMLList = XML(event.result).patientPHP;
            var xmlListColl = new XMLListCollection(xmlList);


            if(xmlListColl.length==1)
            {

                _phppaIndex = xmlListColl.getItemAt(0).paIndex;
                _phppaNomU = xmlListColl.getItemAt(0).paNomU;
                _phppaPrenom = xmlListColl.getItemAt(0).paPrenom;

                var evtPat:Event = new Event("evtPatSelect");

                var evdips:EventDispatcher = new EventDispatcher();
                evdips.dispatchEvent(evtPat);

            }
        }



        private function serviceFault( event:FaultEvent )
        {
            trace( event.fault.message );
        }

        public function get phppaIndex():String
        {
            return _phppaIndex;
        }

        public function set phppaIndex(value:String):void
        {
            _phppaIndex = value;
        }

        public function get phppaNomU():String
        {
            return _phppaNomU;
        }

        public function set phppaNomU(value:String):void
        {
            _phppaNomU = value;
        }

        public function get phppaPrenom():String
        {
            return _phppaPrenom;
        }

        public function set phppaPrenom(value:String):void
        {
            _phppaPrenom = value;
        }


    }
}

在主窗口中我添加了一个eventlistener,但这似乎不起作用。 你能帮我解决一下吗?

由于

1 个答案:

答案 0 :(得分:0)

首先,将sPatient类扩展为EventDispatcher

public class sPatient extends EventDispatcher {

然后,为您的自定义事件创建一个类

public class MyCustomEvent extends Event {
        public static const CUSTOM_TITLE:String = "custom_title";
        public var eventData:Object;

        public function CustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false, data:Object = null) {
            super(type, bubbles, cancelable);
            if(data != null) eventData = data;
        }

        public override function clone():Event {
            return new CustomEvent(type, bubbles, cancelable);
        }

        public override function toString():String {
            return formatToString("CustomEvent", "type", "bubbles", "cancelable", "eventPhase");
        }
    }

然后在您的sPatient课程中,转到:

this.dispatchEvent(new MyCustomEvent(MyCustomEvent.CUSTOM_TITLE));

并像这样倾听

sPatientInstance.addEventListener(MyCustomEvent.CUSTOM_TITLE,functionHandler);