2009-05-07 3 views
0

Ich habe eine Singleton-Klasse für den globalen Zugriff auf Konfigurationsinformationen. Diese Singleton-Klasse namens ConfigurationData erweitert EventDispatcher. Hier ist die Klasse (beachten Sie, dass ich einige Dinge links wie Variablendeklarationen aus diesen kurz zu halten):Flex/AS3 Problem beim Abhören eines Ereignisses, das von einer Singleton-Klasse ausgelöst wurde

/** 
* Dispatched when the config file has been loaded. 
*/ 
[Event (name="configurationLoaded", type="flash.events.Event")] 

/** 
* Dispatched when the config file has been loaded. 
*/ 
[Event (name="configurationLoadFailed", type="flash.events.Event")] 

public class ConfigurationData extends EventDispatcher{ 
    // Event name constants. 
    public static const CONFIGURATION_LOADED:String = "configurationLoaded"; 
    public static const CONFIGURATION_LOAD_FAILED:String = "configurationLoadFailed"; 

    // The singleton instance. 
    private static var singleton:ConfigurationData; 

    /** 
    * Don't call the constructor directly, use getInstance() instead. 
    */ 
    public function ConfigurationData(pvt:PrivateClass){ 
     // init 
    } 

    /** 
    * Get the singleton ConfigurationData. 
    * @return The ConfigurationData instance. 
    */ 
    public static function getInstance():ConfigurationData{ 
      if (!ConfigurationData.singleton) ConfigurationData.singleton = new ConfigurationData(new PrivateClass()); 
      return ConfigurationData.singleton; 
     } 

     public function initialize():void{ 
     var configureService:HTTPService = new HTTPService; 
     configureService.url = _config_base_url + _config_path; 
     configureService.addEventListener(FaultEvent.FAULT, onConfigureFault); 
     configureService.addEventListener(ResultEvent.RESULT, onConfigureResult); 
     configureService.send(); 
     } 

     private function onConfigureResult(event:ResultEvent):void{ 
     var i:int = 0; 
     for(i=0; i<event.result.carriers.carrier.length; i++){ 
      _mobile_carriers.addItem({label:event.result.carriers.carrier[i].name, data:event.result.carriers.carrier[i].id}); 
     } 
     dispatchEvent(new Event(CONFIGURATION_LOADED)); 
    } 

    private function onConfigureFault(event:FaultEvent):void{ 
     _mobile_carriers = _default_carriers as ArrayCollection; 
     dispatchEvent(new Event(CONFIGURATION_LOAD_FAILED)); 
    } 
} 

// This class is used to ensure that the ConfigurationData constructor can't be called directly, 
// getInstance() must be used instead. 
class PrivateClass { 
    public function PrivateClass() {} 
} 

Dann habe ich eine MXML-Komponente, die für die CONFIGURATION_LOADED Ereignis hört:

ConfigurationData.getInstance().addEventListener(Event.CONFIGURATION_LOADED, onConfigurationLoaded); 

Für einige Dies führt zu folgendem Fehler: 1119: Zugriff der möglicherweise undefinierten Eigenschaft CONFIGURATION_LOADED durch eine Referenz mit statischer Klasse.

Kann jemand das beheben, damit ich auf das Ereignis hören kann?

Danke!

Antwort

6

Sie versuchen, auf ein statisches const für die Event-Klasse zuzugreifen, die nicht existiert: Event.CONFIGURATION_LOADED.

In diesem Fall werden Sie eine benutzerdefinierte Event-Klasse erstellen:

public class ConfigurationEvent extends Event 
{ 
    public static const CONFIGURATION_LOADED:String = "configurationLoaded"; 
    public static const CONFIGURATION_LOAD_FAILED:String = "configurationLoadFailed"; 

    public function ConfigurationEvent(type:String) 
    { 
     super(type); 
    } 
} 

Versand und hören Sie diese kundenspezifischen Events statt Event.CONFIGURATION_LOADED:

dispatchEvent(new ConfigurationEvent(ConfigurationEvent.CONFIGURATION_LOAD_FAILED)); 

ConfigurationData.getInstance().addEventListener(ConfigurationEvent.CONFIGURATION_LOADED, onConfigurationLoaded); 
+0

Super, danke! Ich wusste, dass dies das Problem war, aber ich wusste nicht den besten Weg, es zu lösen. Übrigens, vielleicht könntest du dir meine andere Flex-Ausgabe ansehen? http://StackOverflow.com/Questions/825377/Scroll-thumb-is-Extending-Beyond-Scroll-Track-in-Flex-App – Tony

Verwandte Themen