Losing Data When Drag and Dropping Custom Classes in Flex?
One of our developers recently encountered some strange behavior when he was dragging a piece of data (which was a custom class) from a TileList
and dropping it into a List
, but losing some pieces of the dragged data when it was dropped into the List
. I discovered the root of the issue by delving into the source for the dragDropHandler
function within ListBase
...
the first clue was at line 9203 in the mx.controls.listClasses.ListBase
class:
collectionIterator.insert(copyItemWithUID(items[i]));
Ah ha, now on to see what copyItemWithUID
does, so looking at line 9230:
var copyObj:Object = ObjectUtil.copy(item);
and the trail ends in mx.utils.ObjectUtil
at line 102 in the copy function:
var result:Object = buffer.readObject();
The Problem and the Solution: Serialization
Here I discovered the source of the problem. The class that was being dragged was being properly serialized, but it had a property within it which was another custom class, and sure enough that class wasn't being serialized. This was causing that property to be lost - more specifically, it was becoming an empty Object.
If you don't care how your class is serialized, but only that it will be properly serialized within Flex, then simply add the [RemoteClass]
metadata tag above the class definition as follows:
package com.example {
[RemoteClass]
public class MySerializableClass {
...
}
}