Select to view content in your preferred language

Cloning Graphics and other ESRI Objects

2612
3
10-08-2010 06:51 AM
PeterMcAlenney
New Contributor II
HI,

Often times i need to copy an Array of Graphics and it is not that easy.  For some reason the ESRI objects fail when you use the ObjectUtil.copy() function.  Below is a bit more code, consisting of 3 functions which is clone your Graphic objects (and anything else) when you needs to get deep clones or copies.

Let me know if you have any questions!

Thanks,
Pete

                 // These following 3 functions are used to deep
   // clone objects.
   public static function deepClone(source:Object):Object {  
    var clone:Object;
    if(source) {
        clone = newSibling(source);   
        if(clone) {
            copyData(source, clone);
        }
    }   
    return clone;
   }
  
      private static function newSibling(sourceObj:Object):* {
          if(sourceObj) { 
              var objSibling:*;
              try {
                  var classOfSourceObj:Class = getDefinitionByName(getQualifiedClassName(sourceObj)) as Class;
                  objSibling = new classOfSourceObj();
              } 
              catch(e:Object) {} 
              return objSibling;
          }
          return null;
      }
     
      private static function copyData(source:Object, destination:Object):void {
 
          //copies data from commonly named properties and getter/setter pairs
          if((source) && (destination)) {
 
              try {
                  var sourceInfo:XML = describeType(source);
                  var prop:XML;
 
                  for each(prop in sourceInfo.variable) {
 
                      if(destination.hasOwnProperty(prop.@name)) {
                          destination[prop.@name] = source[prop.@name];
                      }
                  }
 
                  for each(prop in sourceInfo.accessor) {
                      if(prop.@access == "readwrite") {
                          if(destination.hasOwnProperty(prop.@name)) {
                              destination[prop.@name] = source[prop.@name];
                          }
                      }
                  }
              }
              catch (err:Object) {
                  ;
              }
          }
      }
Tags (2)
0 Kudos
3 Replies
FrankRoberts
Occasional Contributor III
Can you explain how I would use this to clone a com.esri.ags.Map?  I can put a map into it using:

var mymap:Object = deepClone(map);


But then I'm unclear how to convert the output "mymap" back into a map instead of an object.

Thanks for your help,
Frank
0 Kudos
PeterMcAlenney
New Contributor II
var mymap:Map = deepClone(map) as Map;

That should do it...
0 Kudos
JohnGarvey
New Contributor
Thanks for the utils code.

Whenever I clone the map and add the clone to the display list (in this case within a flexviewer widget), both the original map and the clone are whited-out.

Sorry I don't have only a better description, but any help in diagnosing or solving is much appreciated.

Thanks
0 Kudos