Scenario for you... What if I have a parent object that I use deepClone on and it has child elements that have event handlers, is this functionality preserved with the method?
On Aug 26, 1:18 pm, watermark86 <[email protected]> wrote: > Well, I have x-mas presents. I give it "it works for me" status. > I've tested this bit of code with firefox and it seems to work with > every scenario I can throw at it. I'm sure there is room for speed > improvements and I know it doesn't conform to coding style, but it > works. It would be great if prototype officially extended > Element.clone with this functionality. > > 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; > } > > On Aug 26, 9:43 am, watermark86 <[email protected]> wrote: > > > 1.6.1rc3 added Element.clone (which calls cloneNode). > > > On Aug 25, 5:46 pm, Matt Foster <[email protected]> wrote: > > > > Good luck with this, let us know if it works out. > > > > Quick glance at your code I can see an obvious error... > > > > > var newElement = element.clone(true); //clone the node > > > > Element doesn't have a clone method. The native DOM element does have > > > a cloneNode method though. > > > > -- > > > >http://positionabsolute.net > > > > On Aug 25, 2:15 pm, watermark86 <[email protected]> wrote: > > > > > I'm using 1.6.1rc3 to have access to Element.storage. I need to write > > > > a function that deep clones an element and copies the Element.observe > > > > events as well as the Element.Storage items with it. I've managed to > > > > write the code that can copy one or the other. > > > > > The problem comes in when I try to do both (copy the storage items and > > > > the events.) I suppose due to Event.observe using the Element.storage > > > > item, I can't seem to have the items that I've stored with > > > > Element.store to copy while maintaining the Event.observe events that > > > > I've copied. > > > > > I can copy the observe events or the element.store events, but not > > > > both. Below is some terrible code for review. Thanks for any help or > > > > insults. > > > > > deepClone: function(element) { > > > > //deep clone node > > > > var newElement = element.clone(true); //clone the node > > > > > //stop observing because of IE BS > > > > newElement.stopObserving(); > > > > > //copy the item > > > > var registry = Element.retrieve(element, > > > > 'prototype_event_registry'); > > > > if (!Object.isUndefined(registry)) { > > > > registry.each( function(pair) { > > > > var eventName = pair.key, responders = > > > > pair.value; > > > > responders.each( function(r) { > > > > Element.observe(newElement, eventName, > > > > r.handler); > > > > }); > > > > }); > > > > } > > > > > //for all of the descendants, copy the event handlers > > > > var srcdecend = element.descendants(); > > > > var dstdecend = newElement.descendants(); > > > > var numdecend = srcdecend.length; > > > > > for(var i = 0; i < numdecend; ++i) { > > > > //stop observing because of IE BS > > > > dstdecend[i].stopObserving(); > > > > //copy the registry > > > > 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 & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
