0

我有以下单例:

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

}

当我尝试将两个单例的属性 ServerURL 和 UserID 绑定到我的 HTTPService 的属性 URL 和 Request 中时,这些值在被修改后不会在 HTTPService 中更新。进行绑定的代码如下:

<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>

当我执行 getStatusService.send() 时,它转到“null/getObject”。

任何人都知道为什么变量没有在 HTTPService 中更新?

谢谢您的帮助。

塞巴斯蒂安

4

1 回答 1

0

internalConfiguration.ServerURL 没有被更新,因为从不调用 initialize() 方法(至少在您提供的代码中)。ServerURL 字段默认为空,因此它在 httpservice 中为空。考虑到 ServerURL 应该是可绑定的。ServerURL 也应该有 setter,否则绑定不会被触发。

并且不要忘记配置必须是可绑定的:

[Bindable]
private var internalConfiguration:AppConfiguration = AppConfiguration.getInstance();
于 2013-11-11T23:02:30.190 回答