Flex- Singleton的属性Binded到HttpService属性没有更新

时间:2013-11-11 21:28:08

标签: flex events binding actionscript httpservice

我有以下单身人士:

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绑定到属性URL和我的HTTPService的请求时,这些值在修改后不会在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中更新?

感谢您的帮助。

圣塞瓦斯蒂安

1 个答案:

答案 0 :(得分:0)

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

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

[Bindable]
private var internalConfiguration:AppConfiguration = AppConfiguration.getInstance();