Hi,

FWIW, I don't think this is should be core. It may be useful in the
scenario in which you're using it, which is great, but I see too many
dangers involved in copying event handlers from one element to
another. I mentioned one of them here.[1]

[1] 
http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/e5286fee9ac18a40

Happy coding,
--
T.J. Crowder
tj / crowder software / com
www.crowdersoftware.com

On Aug 27, 5:36 pm, watermark86 <watermar...@gmail.com> wrote:
> I posted this code on the support list, but now that it's working, it
> seems more appropriate to put it here.  Using 1.6.1rc3, the following
> bit of code will deep clone an element along with the events that are
> attached to it and the data stored with Element.store.  I've tested it
> in Firefox 3.5 with every scenario I can think of and it seems to work
> fine.  I propose similar functionality be added to the Element.clone
> method.
>
> deepClone: function(element) {
>         var newElement = element.clone(true); //clone the node
>
>         //stop observing all events on the newElement (IE (and some
> others?) copies them)
>         newElement.stopObserving();
>
>         //copy storage
>         Element.getStorage(newElement);
>         (Element.Storage[newElement._prototypeUID[0]]=new Hash
> (Element.getStorage(element))).unset('prototype_event_registry');
>
>         //copy the events on the parent
>         if (!Object.isUndefined(registry = Element.retrieve(element,
> 'prototype_event_registry'))) {
>                 registry.each( function(pair) {
>                         var eventName = pair.key, responders = pair.value;
>                         responders.each( function(r) {
>                                 Element.observe(newElement, eventName, 
> r.handler);
>                 });
>                 });
>         }
>
>         //get all of the descendants
>     var srcdecend = element.descendants();
>     var dstdecend = newElement.descendants();
>     var numdecend = srcdecend.length;
>
>     //foreach of the descendants
>     for(var i = 0; i < numdecend; ++i) {
>         //stop observing all events on the newElement (IE (and some
> others?) copies them)
>                 dstdecend[i].stopObserving();
>                 //copy storage
>                 Element.getStorage(dstdecend[i]);
>                 
> (Element.Storage[dstdecend[i]._prototypeUID[0]]=Element.getStorage
> (srcdecend[i]).clone()).unset('prototype_event_registry');
>         //copy the events on each child to it's new corrisponding child
>         var registry = Element.retrieve(srcdecend[i],
> 'prototype_event_registry');
>                 if (!Object.isUndefined(registry)) {
>                         registry.each( function(pair) {
>                                 var eventName = pair.key, responders = 
> pair.value;
>                                 responders.each( function(r) {
>                                         Element.observe(dstdecend[i], 
> eventName, r.handler);
>                         });
>                         });
>                 }
>     }
>
>     return newElement;
>   }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to