jus*_*vin 4 apache-flex actionscript-3
通常在Flex中,您使用ArrayCollection之类的集合作为组件的数据提供者.我经常将数据存储为字典,其值我想用作数据提供者.
你会怎么推荐这样做?
理想的解决方案是不使用字典,但不能回答你的问题......
所以,如果你被迫使用词典,解决这个问题的一种方法就是使用一个函数作为你的"中间人".它可以在您的字典和关注何时更改的Flex组件之间进行.
当然,对于大型词典,这可能效率低下,但在大多数情况下,性能应该是可以接受的.
首先,您需要一个函数来从Dictionary转换为ArrayCollection.以下工作示例之一就足够了:
从字典中提取键/值集合
public function extractKeys(d:Dictionary):ArrayCollection {
var keyList:ArrayCollection = new ArrayCollection();
if(d != null) for(var key:* in d) keyList.addItem(key);
return keyList;
}
public function extractValues(d:Dictionary):ArrayCollection {
var valueList:ArrayCollection = new ArrayCollection();
if(d != null) for each(var value:* in d) valueList.addItem(value);
return valueList;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以实际绑定到这些函数本身!这样,只要修改了基础字典,任何依赖于这些函数的对象都将得到更新.例如:
使用函数绑定到字典值
private var myDictionary:Dictionary; //initialized and used elsewhere
[Bindable(event="dictionaryValueChanged")]
public function extractValues(d:Dictionary):ArrayCollection {
var valueList:ArrayCollection = new ArrayCollection();
if(d != null) for each(var value:* in d) valueList.addItem(value);
return valueList;
}
public function updateDictionary(key:Object, value:Object):void {
myDictionary[key] = value;
dispatchEvent("dictionaryValueChanged");
}
Run Code Online (Sandbox Code Playgroud)
从那里你可以使用THE FUNCTION作为你的数据提供者.如:
<mx:DataGrid id="valueGrid" dataProvider="{extractValues()}" />
Run Code Online (Sandbox Code Playgroud)
每当你的字典改变时(通过updateDictionary函数),它最终会触发DataGrid中的更新!
这实际上只是flex中非常强大的技术的应用:绑定到函数.一旦掌握了它,就可以通过这种方法解决各种各样的问题.
我希望能帮到某人,
-gMale
编辑:
我不得不玩它,但我认为你也可以通过做这样的事情来消除'updateDictionary'功能,而不是:
[Bindable(event="dictionaryValueChanged")]
private var myDictionary:Dictionary; //initialized and used elsewhere
[Bindable(event="dictionaryValueChanged")]
public function extractValues(d:Dictionary):ArrayCollection {
var valueList:ArrayCollection = new ArrayCollection();
if(d != null) for each(var value:* in d) valueList.addItem(value);
return valueList;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4793 次 |
| 最近记录: |