flex序列化

2. Flex前台
2.1 加载数据:通过HTTPService从后台加载XML数据,这里将当前子网的ID传给了后台。数据获取后,通过SerializeAdapter.deserialize方法将XML数据反序列化。为了实现延迟加载,这里在network上添加了Interaction事件监听。

1
network.addInteractionListener(function(e:InteractionEvent):void {
2
    if (e.kind == InteractionEvent.ENTER_SUBNETWORK) {
3
        if (network.currentSubNetwork.getClient("LOADED") != "true") {
4
            loadCurrentSubnetwork();
5
            network.currentSubNetwork.setClient("LOADED", "true");
6
        }
7
    }
8
});
01
private function loadCurrentSubnetwork():void{
02
    var service:HTTPService = new HTTPService();
03
    service.resultFormat = "e4x";
04
    service.url = URL;
05
    service.addEventListener("result", function(e:*):void{
06
        if(e.result != null){
07
            serializer.deserialize(e.result, network.currentSubNetwork);
08
        }
09
    });
10
    service.addEventListener("fault", function(e:*):void{
11
        trace(e);
12
    });
13
    var params:Object = new Object();
14
    if(network.currentSubNetwork != null){
15
        params.currentSubNetwork = network.currentSubNetwork.id.toString();
16
    }
17
    service.send(params);
18
}
2.2 保存数据:通过SerializeAdapter.serialize方法将Topo数据序列化成XML数据,用HTTPService保存XML数据,这里将当前子网的ID传给了后台。通过SerializeAdapter.filterFunction,可以过滤非当前子网的网元。

1
serializer.filterFunction = function(data:IData):Boolean {
2
    return Utils.getSubNetwork(data as IElement) == network.currentSubNetwork;
3
};
01
private function saveCurrentSubnetwork():void{
var service:HTTPService = new HTTPService();
03
    service.resultFormat = "e4x";
04
    service.url = URL;
05
    service.addEventListener("fault", function(e:*):void{
06
        trace(e);
07
    });
08
    var params:Object = new Object();
09
    params.action = "save";
10
    if(network.currentSubNetwork != null){
11
        params.currentSubNetwork = network.currentSubNetwork.id.toString();
12
    }
13
    params.topoXml = encodeURIComponent(serializer.serialize());
14
    service.send(params);
15
}
2.3 默认只序列化TWaver提供的默认网元,比如Node、Link等。如果要序列化从TWaver基类继承的自定义类,需要添加如下代码:

01
//注册自定义类
02
SerializeAdapterSetting.registerGlobalClass("CNode", FlexNode);
03
//注册自定义属性
04
SerializeAdapterSetting.registerGlobalProperty("customProperty", Consts.TYPE_STRING, true);
05
//注册自定义Client属性
06
SerializeAdapterSetting.registerGlobalClient(FlexNode.CUSTOM_CLIENT, Consts.TYPE_STRING);
07
//注册序列化方法
08
SerializeAdapterSetting.registerGlobalXMLSerializeFunction(FlexNode,
09
    function(serializer:SerializeAdapter, instance:Object, newInstance:Object):void{
10
        serializer.serializeProperty(instance, "customProperty", newInstance);
11
});

你可能感兴趣的:(xml,Flex)