0

I have the following Singleton:

package singletons{

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.net.SharedObject;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.registerClassAlias;


public class AppConfiguration extends EventDispatcher
{
    public static const SUCCESSFULCONFIGURATION:String = "ConfigurationSuccessful";
    public static const CONFIGURATIONERROR:String = "ConfigurationError";

    private var _userID:Number;     
    private var _serverIP:String;       
    private var _serverPort:Number;     
    private var _serverURL:String;
    private static var _instance:AppConfiguration; 


    public static function getInstance():AppConfiguration
    {
        if (_instance == null) 
        {
            _instance = new AppConfiguration();
            registerClassAlias( "singletons.AppConfiguration", AppConfiguration );
        }

        return _instance;
    }       

    public function initialize():Boolean
    {           
        var configFile:SharedObject = SharedObject.getLocal("InternalConfiguration");


        if (configFile.data.internalConfiguration == null) 
        {               
            return false;
        }           

        var localConf:AppConfiguration =  AppConfiguration(configFile.data.internalConfiguration);
        _userID = localConf.UserID;
        _serverIP = localConf.ServerIP;
        _serverPort = localConf.ServerPort;
        _serverURL = "http://" + _serverIP + ":" + _serverPort;

        return true;        
    }

    public function setInternalConfiguration(userID:Number, serverIP:String, serverPort:Number):void
    {
        _userID = userID;
        _serverIP = serverIP;
        _serverPort = serverPort;
        _serverURL = "http://" + _serverIP + ":" + _serverPort;

        var configurationRequest:URLRequest = new URLRequest(_serverURL + "/getHello?userID=" + _userID);
        var requestLoader:URLLoader = new URLLoader();
        requestLoader.addEventListener(Event.COMPLETE, getConfigurationHandler);
        requestLoader.addEventListener(IOErrorEvent.IO_ERROR, getConfigurationErrorHandler);
        requestLoader.load(configurationRequest);
    }

    private function getConfigurationHandler(event:Event):void
    {
        // 1.2.- Asignar los valores de las variables obtenidas a las variables locales


        var configFile:SharedObject = SharedObject.getLocal("InternalConfiguration");
        configFile.data.internalConfiguration = this as AppConfiguration;
        configFile.flush();

        var successEvent:Event = new Event(SUCCESSFULCONFIGURATION);
        dispatchEvent(successEvent);            
    }

    private function getConfigurationErrorHandler(event:IOErrorEvent):void
    {
        var failedEvent:Event = new Event(CONFIGURATIONERROR);
        dispatchEvent(failedEvent);                 
    }


    public function get UserID():Number 
    {           
        return _userID;
    }

    public function get ServerIP():String 
    {           
        return _serverIP;
    }

    public function get ServerPort():Number 
    {           
        return _serverPort;
    }       
    public function get ServerURL():String 
    {           
        return _serverURL;
    }       
}

}

And when I try to bind two singleton's properties ServerURL and UserID into the properties URL and Request of my HTTPService, the values don't get updated in the HTTPService after they have being modified. The code that makes the binding is the following:

<fx:Declarations>
    <s:HTTPService id="getStatusService"
                   url="{internalConfiguration.ServerURL}/getObject"
                   result="getStatusHandler(event)" fault="getStatusErrorHandler(event)">
        <s:request>
            <codec>{internalConfiguration.CodecID}</codec>
        </s:request>
    </s:HTTPService>                    
</fx:Declarations>

And I when I do the getStatusService.send(), it goes to "null/getObject".

Anyone knows why the variables are not being updated inside the HTTPService?

Thanks for the help.

Sebastián

SebastianT
  • 263
  • 3
  • 14

1 Answers1

0

internalConfiguration.ServerURL is not being updated as initialize() method is never called (at least in code you've provided.) ServerURL field is empty by default so that is why it's null in httpservice. Take into account that ServerURL should be bindable. Also ServerURL should has setter, otherwise binding will not be triggered.

And don't forget that config has to be bindable either:

[Bindable]
private var internalConfiguration:AppConfiguration = AppConfiguration.getInstance();
  • Thanks Daniil for you comments. I didn't mentioned it but the `initialize()` method is being called at `CreationComplete`. I checked the values after the `getConfigurationHandler(event:Event)` is called and all the values are updated. Als, I declared the `internalConfiguration` as `[Bindable]`. I really don't understand why this is. If you have any more ideas I would really appreciate it. Have a good week! – SebastianT Nov 12 '13 at 11:34
  • Like I said ServerURL should has setter. And also getter of ServerURL should be bindable. And inside of initialize() method call setter of ServerURL but not private field _serverURL then it will work. – Daniil Moskovtsov Nov 12 '13 at 15:33