Joe,
Your timing on this was impeccable!  I just spent the past hour
wrestling with ObjectCopy (both Manish's and the mx.utils one that
doesn't work).

So here is Manish's ObjectCopy with Joe's hack to preserve type:

  public static function copy(source):Object
  {
    var cl:Number = copyCache.length;
    for (var i:Number = 0; i < cl; i++)
    {
      var o = copyCache[i];
      if (o.s == source)
        return o.t;
    }

    copyDepthLevel++;

    var newObj;

    if (typeof(source.__constructor__) == "function")
    {
      // for AS2 classes
      newObj = new source.__constructor__();
    }
    else if (typeof(source.constructor) == "function")
    {
      // for Arrays and Objects
      newObj = new source.constructor();
    }
    else
    {
      // for goodness sake, instantiate *something*!
      newObj = new Object();
    }

    copyCache.push({s: source, t: newObj});

    for (var p in source)
    {
      var v = source[p];
      newObj[p] = typeof v == "object" ? copy(v) : v;
    }

    if (--copyDepthLevel == 0)
      copyCache = [];

    return newObj;
  }


Notice that the method signature changed slightly.  This was to avoid
this error:
Error: A function call on a non-function was attempted.

due to: 
newObj = new source.constructor();


I have tested this code in my app and it's working great!  Thanks Joe
and Manish!

-James


On Wed, 2005-06-22 at 11:18 -0400, Joe Berkovitz wrote:
> A couple of big caveats here.
> 
> 1. mx.utils.ObjectCopy is broken.  (At least, in the Flex 1.5 
> distribution.)  It does not correctly preserve the class of an object 
> when that class is a subclass of some superclass.  Instead, the copied 
> object will appear to have the type of the superclass.
> 
> I note that ObjectCopy appears to only be used in net debugging and 
> nowhere else.  So this bug isn't exposed via any documented Flex APIs.
> 
> Moral: beware of undocumented internal Flash classes, however convenient 
> they might look.
> 
> 2. Even if ObjectCopy worked, you cannot conveniently meld Manish's 
> cached-array approach with it, because ObjectCopy will do the wrong 
> thing with cycles (it locks up) and multiply referenced objects (it 
> duplicates instances).
> 
> So.... what to do?
> 
> As empirically determined through testing, one correct approach in Flex 
> 1.5 to instantiating an object that preserves the type of an existing 
> one is as follows:
> 
>          var newObj;
>          if (typeof(obj.__constructor__) == "function")
>          {
>              // for AS2 classes
>              newObj = new obj.__constructor__();
>          }
>          else if (typeof(obj.constructor) == "function")
>          {
>              // for Arrays and Objects
>              newObj = new obj.constructor();
>          }
>          else
>          {
>              // for goodness sake, instantiate *something*!
>              newObj = new Object();
>          }
> 
> I've exhaustively tested this and it works correctly for AS2 classes, 
> for Arrays, and for untyped Objects.  You can merge it with Manish's 
> code (which I haven't tested, but looks good) to produce a working, 
> accurate deep copy.
> 
> .. .  .    .       .            j
> 
> 
> Abdul Qabiz wrote:
> > http://manish.revise.org/2005/04/deepcopying-actionscript-objects.html
> > 
> > http://www.darronschall.com/weblog/archives/000148.cfm 
> > 
> > 
> > -abdul
> > 
> > 
> > -----Original Message-----
> > From: [email protected] [mailto:[EMAIL PROTECTED] On
> > Behalf Of Pradeep Chaudhary
> > Sent: Wednesday, June 22, 2005 7:33 PM
> > To: [email protected]
> > Subject: [flexcoders] How to create copy of a object
> > 
> > I want to create a copy for my custom object before it is modified so
> > that i can rollback my changes on any error condition. Do we have any
> > inbuilt methods or any alternative solution for creating a copy of
> > object so that changes made to original object is not reflected copied
> > object.
> > 
> > Pradeep
> > 
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> > 
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> > 
> > 
> > 
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 




 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to