views:

1010

answers:

1

I have a singleton class for global access to config information. This singleton class called ConfigurationData extends EventDispatcher. Here is the class (note that I left some things out like variable declarations to keep this short):

/**
* 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() {}
}

Then I have an MXML component which listens for the CONFIGURATION_LOADED event:

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

For some reason this produces the following error: 1119: Access of possibly undefined property CONFIGURATION_LOADED through a reference with static type Class.

Does anyone know how to fix this so I can listen for the event?

Thanks!

+2  A: 

You're trying to access a static const on the Event class that doesn't exist: Event.CONFIGURATION_LOADED.

In this case, you'd want to create a custom Event class:

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

Dispatch and listen to these custom Events instead of Event.CONFIGURATION_LOADED:

dispatchEvent(new ConfigurationEvent(ConfigurationEvent.CONFIGURATION_LOAD_FAILED));

ConfigurationData.getInstance().addEventListener(ConfigurationEvent.CONFIGURATION_LOADED, onConfigurationLoaded);
Stiggler
Awesome, thank you! I knew this was the problem but I didn't know the best way to solve it. Btw, maybe you could take a look at my other Flex issue? http://stackoverflow.com/questions/825377/scroll-thumb-is-extending-beyond-scroll-track-in-flex-app
Tony