(编辑:我已经编辑了我的问题以使其更简单,如果某些答案脱离上下文,对不起)
我为我的问题准备了一个简化的测试用例:
我正在尝试创建一个自定义组件,该组件正在接收来自服务器的 XML 数据。我的问题是 CollectionEvent 侦听器没有被触发,因此标签没有更新 -
Games.mxml(我的带有监听器的自定义组件):
<mx:Script>
<![CDATA[
import mx.events.*;
private var _xlist:XMLList;
[Bindable]
public function get xlist():XMLList {
return _xlist;
}
public function set xlist(x:XMLList):void {
_xlist = x;
trace("set(" + x +")");
list.dataProvider = x;
list.dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, xlistChanged);
}
private function gameLabel(item:Object):String {
return "game: " + item.@label;
}
private function xlistChanged(event:CollectionEvent):void {
trace("xlistChanged(" + event +")");
all.text = "All games: " + _xlist.game.length();
full.text = "Full games: " + _xlist.game.(user.length() == 3).length();
vacant.text = "Vacant games: " + _xlist.game.(user.length() < 3).length();
}
]]>
</mx:Script>
<mx:Label id="all" text="All games"/>
<mx:Label id="full" text="Full games"/>
<mx:Label id="vacant" text="Vacant games"/>
<mx:List id="list" labelFunction="gameLabel"/>
MyTest.mxml(单击按钮更改 XML):
private function changeXML1():void {
games = <games>
<game label="1">
<user/>
<user/>
<user/>
</game>
<game label="2">
<user/>
<user/>
</game>
<game label="3">
<user/>
<user/>
<user/>
</game>
</games>;
}
private function changeXML2():void {
games = <games>
<game label="A">
<user/>
<user/>
<user/>
</game>
<game label="B">
<user/>
<user/>
</game>
<game label="C">
</game>
</games>;
}
]]>
</mx:Script>
<mx:XML id="games">
<games>
<game label="X">
<user/>
<user/>
</game>
<game label="Y">
<user/>
<user/>
</game>
</games>
</mx:XML>
<mx:Button label="Change XML 1" click="changeXML1()"/>
<mx:Button label="Change XML 2" click="changeXML2()"/>
<my:Games xlist="{games.game}"/>
请告诉我有什么问题。
问候,亚历克斯
更新:按照 clownbaby 的建议编辑 Games.mxml - 仍然无法正常工作(从未调用 xlistChanged):
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:my="*" creationComplete="onCreationComplete(event)">
<mx:Script>
<![CDATA[
import mx.events.*;
private var _xlist:XMLList;
[Bindable]
public function get xlist():XMLList {
return _xlist;
}
public function set xlist(x:XMLList):void {
_xlist = x;
list.dataProvider = x;
trace("\n set: " + x);
}
private function gameLabel(item:Object):String {
return "game: " + item.@label;
}
private function onCreationComplete(event:FlexEvent):void {
list.dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, xlistChanged);
}
private function xlistChanged(event:CollectionEvent):void {
all.text = "All games: " + xlist.length();
full.text = "Full games: " + xlist.(user.length() == 3).length();
vacant.text = "Vacant games: " + xlist.(user.length() < 3).length();
}
]]>
</mx:Script>
<mx:Label id="all" text="All games"/>
<mx:Label id="full" text="Full games"/>
<mx:Label id="vacant" text="Vacant games"/>
<mx:List id="list" labelFunction="gameLabel"/>
</mx:VBox>