|
Bgx Xml Serialization Classes (Flash MX 2004) :
Using Recursive Cloning
Flash output
For general details about the XML used and binding it to a DataGrid
see the DataGrid example
Why cloning?
The example above uses two main DataGrids that are bound to an
Array in our deserialized sample data. Both of them have a second
DataGrid associated that allows to edit the data (click on a row
to edit it).
In the first example both DataGrids point to the same Array SampleData.folder[1].folder[0].page.
As they both use literally the same Object
any changes made on the right will immediately be reflected on the
left (Note also Main DataGrid 2 will reflect those changes before
clicking "Apply Changes" once because it is also bound
to the same Object).
Now in many cases you would want to allow the user to make changes
but only apply them to your main Object once confirmed or otherwise
to cancel them.
In this case you need to create a clone of your original data when
transfering DataProviders from one grid to the other.
The BgxDesObject class now has a powerful recursive cloning function
that you can use to clone the entire object itself by simply calling
instanceBgxDesObject.clone(); or to clone any other
complex Object or Array that you pass like
instanceBgxDesObject.clone(instanceBgxDesObject.child[0].property);
instanceBgxDesObject.clone(dgPagesCloned.dataProvider);
You can pass either Arrays or Objects to the clone()
function and it will return respectively an Array or an Object.
All Objects within the cloned Data will be of type BgxDesObject
and will include automatically a reference to the respective parentNodes
within the cloned data. E.g. the following
var ClonedData = SampleData.clone();
trace (ClonedData.folder[1].folder[0].parentNode.label);
will return "Tutorials & Samples".
You can also pass a reference to another Object that will become
the parentNode of the cloned Object itself
e.g. to link it back into a BgxDesObject, like in the example below:
SampleData.folder[1].folder[0].page =
SampleData.clone(dgPagesCloned.dataProvider,SampleData.folder[1].folder[0]);
trace(SampleData.folder[1].folder[0].page[0].parentNode.label);
will return "Weather webservice".
Note: As the Serialization Classes are mainly
set up to deal with XML compliant data structures and to avoid entering
into infinite loops that could arise if Objects cross reference
each other (like e.g. the parentNode properties
in instances of the BgxDesObject Class), properties of Objects which
are Objects themselves will currently not be included in the clone.
E.g. a clone of the following Object
var testObj = new Object();
testObj["id"] = 1;
testObj["isNew"] = true;
testObj["name"] = "test";
testObj["dataObj"] ={val1:1,val2:2};
testObj["dataArray"] = [{val1:1,val2:2},{val1:3,val2:4}];
will not include the property dataObj.
The Code
import bgx.BgxDesObject;
var SampleData:Object;
var sampleXML:XML = new XML();
sampleXML.ignoreWhite = true;
sampleXML.onLoad = parseSampleData;
sampleXML.load("sample.xml");
function parseSampleData() {
var rootNode:XMLNode = sampleXML.firstChild;
SampleData = new BgxDesObject();
SampleData.xmlNode = rootNode;
SampleData.onParsed = populateGrids;
SampleData.parse();
}
function populateGrids(){
var pagesArray = SampleData.folder[1].folder[0].page;
dgMain1.columnNames= ["label"];
dgMain1.getColumnAt(0).headerText = "Pages";
dgMain2.columnNames= ["label"];
dgMain2.getColumnAt(0).headerText = "Pages";
pagesArray.sortOn("label");
dgMain1.dataProvider = pagesArray;
dgMain2.dataProvider = pagesArray;
dgPagesNoclone.columnNames= ["label"];
dgPagesNoclone.getColumnAt(0).headerText = "Edit Pages";
dgPagesNoclone.dataProvider = pagesArray;
var pagesArrayCloned = SampleData.clone(SampleData.folder[1].folder[0].page);
dgPagesCloned.columnNames= ["label"];
dgPagesCloned.getColumnAt(0).headerText = "Edit Pages";
dgPagesCloned.dataProvider = pagesArrayCloned;
}
btnApply.onRelease = function()
{
SampleData.folder[1].folder[0].page =
SampleData.clone(dgPagesCloned.dataProvider,SampleData.folder[1].folder[0]);
var pagesArray = SampleData.folder[1].folder[0].page;
pagesArray.sortOn("label");
dgMain2.dataProvider = pagesArray;
}
btnCancel.onRelease = function()
{
var pagesArrayCloned = SampleData.clone(SampleData.folder[1].folder[0].page);
dgPagesCloned.dataProvider = pagesArrayCloned;
}
For further information please contact info@bgxcomponents.com.
|