문제

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

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top