最近写一个小工具,用Flex开发,发现一些数据在List/DataGrid中显示时,总是只能选中最后一行.最后发现是因为自定义的数据中使用了uid做成员变量.
uid应该是十分常用的字段了,但是Flex框架也使用了这个变量做各组件的id.设置基于List的组件的dataProvider之后,由于uid重名,自然flex会发生一些异常.
相关代码在mx.utils.UIDUtil中:
public static function getUID(item:Object):String
{
var result:String = null;
if (item == null)
return result;
if (item is IUID)
{
result = IUID(item).uid;
if (result == null || result.length == 0)
{
result = createUID();
IUID(item).uid = result;
}
}
else if ((item is IPropertyChangeNotifier) &&
!(item is IUIComponent))
{
result = IPropertyChangeNotifier(item).uid;
if (result == null || result.length == 0)
{
result = createUID();
IPropertyChangeNotifier(item).uid = result;
}
}
else if (item is String)
{
return item as String;
}
else
{
try
{
// We don't create uids for XMLLists, but if
// there's only a single XML node, we'll extract it.
if (item is XMLList && item.length == 1)
item = item[0];
if (item is XML)
{
// XML nodes carry their UID on the
// function-that-is-a-hashtable they can carry around.
// To decorate an XML node with a UID,
// we need to first initialize it for notification.
// There is a potential performance issue here,
// since notification does have a cost,
// but most use cases for needing a UID on an XML node also
// require listening for change notifications on the node.
var xitem:XML = XML(item);
var nodeKind:String = xitem.nodeKind();
if (nodeKind == "text" || nodeKind == "attribute")
return xitem.toString();
var notificationFunction:Function = xitem.notification();
if (!(notificationFunction is Function))
{
// The xml node hasn't already been initialized
// for notification, so do so now.
notificationFunction =
XMLNotifier.initializeXMLForNotification();
xitem.setNotification(notificationFunction);
}
// Generate a new uid for the node if necessary.
if (notificationFunction["uid"] == undefined)
result = notificationFunction["uid"] = createUID();
result = notificationFunction["uid"];
}
else
{
if ("mx_internal_uid" in item)
return item.mx_internal_uid;
if ("uid" in item)
return item.uid;
result = uidDictionary[item];
if (!result)
{
result = createUID();
try
{
item.mx_internal_uid = result;
}
catch(e:Error)
{
uidDictionary[item] = result;
}
}
}
}
catch(e:Error)
{
result = item.toString();
}
}
return result;
}
问题即出在if ("uid" in item) return item.uid; 这一句上:)
uid应该是十分常用的字段了,但是Flex框架也使用了这个变量做各组件的id.设置基于List的组件的dataProvider之后,由于uid重名,自然flex会发生一些异常.
相关代码在mx.utils.UIDUtil中:
public static function getUID(item:Object):String
{
var result:String = null;
if (item == null)
return result;
if (item is IUID)
{
result = IUID(item).uid;
if (result == null || result.length == 0)
{
result = createUID();
IUID(item).uid = result;
}
}
else if ((item is IPropertyChangeNotifier) &&
!(item is IUIComponent))
{
result = IPropertyChangeNotifier(item).uid;
if (result == null || result.length == 0)
{
result = createUID();
IPropertyChangeNotifier(item).uid = result;
}
}
else if (item is String)
{
return item as String;
}
else
{
try
{
// We don't create uids for XMLLists, but if
// there's only a single XML node, we'll extract it.
if (item is XMLList && item.length == 1)
item = item[0];
if (item is XML)
{
// XML nodes carry their UID on the
// function-that-is-a-hashtable they can carry around.
// To decorate an XML node with a UID,
// we need to first initialize it for notification.
// There is a potential performance issue here,
// since notification does have a cost,
// but most use cases for needing a UID on an XML node also
// require listening for change notifications on the node.
var xitem:XML = XML(item);
var nodeKind:String = xitem.nodeKind();
if (nodeKind == "text" || nodeKind == "attribute")
return xitem.toString();
var notificationFunction:Function = xitem.notification();
if (!(notificationFunction is Function))
{
// The xml node hasn't already been initialized
// for notification, so do so now.
notificationFunction =
XMLNotifier.initializeXMLForNotification();
xitem.setNotification(notificationFunction);
}
// Generate a new uid for the node if necessary.
if (notificationFunction["uid"] == undefined)
result = notificationFunction["uid"] = createUID();
result = notificationFunction["uid"];
}
else
{
if ("mx_internal_uid" in item)
return item.mx_internal_uid;
if ("uid" in item)
return item.uid;
result = uidDictionary[item];
if (!result)
{
result = createUID();
try
{
item.mx_internal_uid = result;
}
catch(e:Error)
{
uidDictionary[item] = result;
}
}
}
}
catch(e:Error)
{
result = item.toString();
}
}
return result;
}
问题即出在if ("uid" in item) return item.uid; 这一句上:)
网友评论(0):


