如何从类中调用外部函数?

时间:2011-08-01 10:53:06

标签: flex actionscript-3

我想在类中调用外部函数。这就是代码;

在checkConnectionStatus函数中,

此[_funcNameForSucceededCon]。适用();不起作用,因为“this”是类,而不是Application。我如何在此时联系应用程序或我该怎么办?

任何帮助将不胜感激。

最好的问候, 米拉。

package myLibrary
{
     import air.net.URLMonitor;

     import flash.events.Event;
     import flash.events.StatusEvent;
     import flash.net.URLRequest;

     public class connectionControl
     {
         private var _urlReq:URLRequest;
         private var _urlMonitor:URLMonitor;

         private var _funcNameForSucceededCon:String;
         private var _funcNameForFailedCon:String;

         public function connectionControl(targetURL:String, funcNameForSucceededCon:String, funcNameForFailedCon:String)
         {
             _urlReq = new URLRequest(targetURL);
             _urlMonitor = new URLMoniotor(_urlReq);

             _urlMonitor.addEventListener(StatusEvent.STATUS, checkConnectionStatus);

             _funcNameForSucceededCon = funcNameForSucceededCon;
             _funcNameForFailedCon = funcNameForFailedCon;

             if(_urlMonitor.running == false)
             {
                 _urlMonitor.start();
             }
             else
             {
                 _urlMonitor.stop();
                 _urlMonitor.start();
             }
         }

         private function checkConnectionStatus(e:Event):void
         {
             _urlMonitor.removeEventListener(StatusEvent.STATUS, checkConnectionStatus);

             if(_urlMonitor.available)
             {
                this[_funcNameForSucceededCon].apply();
             }
             else
             {
                this[_funcNameForFailedCon].apply();
             }

         }

     }
}

1 个答案:

答案 0 :(得分:2)

您已传递要用作回调的函数名称。使用函数本身并将其传递给connectionControl。

 public class connectionControl
 {

     private var _funcSucceededCon:Function;
     private var _funcFailedCon:Function;

     public function connectionControl(targetURL:String, funcSucceededCon:Function, funcFailedCon:Function)
     {
         _urlReq = new URLRequest(targetURL);
         _urlMonitor = new URLMoniotor(_urlReq);

         _urlMonitor.addEventListener(StatusEvent.STATUS, checkConnectionStatus);

         _funcSucceededCon= funcSucceededCon;
         _funcFailedCon= funcFailedCon;

         ...

        if(_urlMonitor.available)
         {
            _funcSucceededCon();
         }