Flex中如何用describeType函数和E4X/XML检查对一个类的访问/存取权限的例子
By Minidxer | September 20, 2008
接下来的例子演示了Flex中如何用describeType函数和E4X/XML,检查对一个类的访问/存取权限。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white"
- initialize="init();">
- <mx:Script>
- <![CDATA[
- import flash.utils.getDefinitionByName;
- import mx.collections.Sort;
- import mx.collections.SortField;
- import mx.collections.XMLListCollection;
- import mx.controls.*;
- import mx.utils.StringUtil;
- import mx.utils.ObjectUtil;
- [Embed("bullet_red.png")]
- private const WRITE_ONLY_ICON:Class;
- [Embed("bullet_yellow.png")]
- private const READ_ONLY_ICON:Class;
- [Embed("bullet_green.png")]
- private const READ_WRITE_ICON:Class;
- private var theXML:XML;
- private var theXMLList:XMLList;
- private var theXMLListColl:XMLListCollection;
- private function init():void {
- var theSortField:SortField = new SortField("@name", true);
- var theSort:Sort = new Sort();
- theSort.fields = [theSortField];
- // var theXML:XML = describeType(getDefinitionByName("mx.controls.Alert"));
- theXML = describeType(Label);
- theXMLList = theXML.factory.accessor.(@declaredBy == theXML.@name);
- theXMLListColl = new XMLListCollection(theXMLList);
- theXMLListColl.sort = theSort;
- theXMLListColl.refresh();
- list.dataProvider = theXMLListColl;
- panel.title = "Accessor methods for the " + theXML.@name + " class:"
- }
- private function list_labelFunc(item:XML):String {
- var itemName:String = item.@name;
- var itemType:String = item.@type.split("::").pop();
- return StringUtil.substitute("{0} : {1}",
- itemName,
- itemType);
- }
- private function list_iconFunc(item:XML):Class {
- var access:String = item.@access;
- switch (access) {
- case "readwrite":
- return READ_WRITE_ICON;
- break;
- case "readonly":
- return READ_ONLY_ICON;
- break;
- case "writeonly":
- return WRITE_ONLY_ICON;
- break;
- default:
- break;
- }
- return null;
- }
- ]]>
- </mx:Script>
- <mx:Panel id="panel" width="350">
- <mx:List id="list"
- labelFunction="list_labelFunc"
- iconFunction="list_iconFunc"
- verticalScrollPolicy="on"
- width="100%"
- itemClick="Alert.show(list.selectedItem.toXMLString());" />
- <mx:ControlBar>
- <mx:Button label="Read/Write" icon="{READ_WRITE_ICON}" skin="{null}" />
- <mx:Button label="Read Only" icon="{READ_ONLY_ICON}" skin="{null}" />
- <mx:Button label="Write Only" icon="{WRITE_ONLY_ICON}" skin="{null}" />
- </mx:ControlBar>
- </mx:Panel>
- </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
No Comments » |
Tags: describeType(), E4X, getDefinitionByName, XML, XMLList, XMLListCollection