On Sep 20, 4:48 am, "Mert Hurturk" <[EMAIL PROTECTED]> wrote:
> Hi all,
> I have a question about event observing and memory management.
>
> -/ Let's say I have a div element on page. On dom load, my app starts to
> observe click event on this element: $('demo-div').observe('click',
> function() { alert('clicked')} );
> -/ Soon after, I remove this div from dom: $('demo-div').remove();
>
> What happens to event handler? Does it still use a segment of memory even
> the element it is assoicated is removed from dom? Maybe this is not an issue
> with a few elements but with large number of elements in a page, it will be
> a problem.

The event handler remains attached, since `remove` actually returns a
reference to a removed element. We can not strip event handlers from
the element that still has references to it, as that would be too
obtrusive. I believe that's a garbage collector's job anyway. It's
worth mentioning that a trunk version of prototype calls
`stopObserving` (on descendant elements) implicitly in `update` method
(but only for IE). This was meant to prevent memory leaks.

If you don't intend to use an element (or its descendants) after
removal, it might make sense to strip all of its observers explicitly:

Element.addMethods({
  purgeObservers: function(element) {
    Element.select((element = $(element)),
'*')._each(Event.stopObserving);
    return element;
  }
});

someElement.purgeObservers().remove();

It could also make sense to let `remove` accept an optional argument.
When being truthy, that argument would force `remove` to strip
observers automatically:

someElement.remove(true); // removes observers
someElement.remove(); // leaves observes untouched

--
kangax
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to