I'm not proposing that the Element.clone have this functionality by
default (I agree there are scenarios when you wouldn't want the event
handlers and the storage item(s) copied.)  What if Element.clone where
to be given another one or two arguments (ie. Element.clone(deepClone,
cloneStorage, cloneEventHandlers)).  Those other two arguments would
decide if the storage and/or event handlers were copied with the
element.

The dangers of cloning event handlers is the same as cloning the
element itself, you just have to understand what you are cloning.  We
can give the user the gun and bullets, but it's up to them to fire at
the target and not their foot.

On Aug 28, 5:09 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> 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/...
>
> Happy coding,
> --
> T.J. Crowder
> tj / crowder software / comwww.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