prototypejs / prototype

Prototype JavaScript framework
http://prototypejs.org/
Other
3.53k stars 639 forks source link

Double update removes event handlers #154

Open jwestbrook opened 10 years ago

jwestbrook commented 10 years ago

previous lighthouse ticket #1692 by Hero Wanders


Calling two times element.update(content) in a row causes event handlers registered on parts of the content via .observe() to vanish.

var e = new Element('span').update('hello');
e.observe('click', function() { alert('world'); });

var test = $('test');

test.update(e); // comment one of these two lines to see that the
test.update(e); // handler gets called only when doing a single update

Handlers directly registered via .addEventListener or .onclick etc. are not affected.

Live example: Failing double update: http://jsfiddle.net/nH6PG/ Working single update: http://jsfiddle.net/BUJyt/

jwestbrook commented 10 years ago

Currently the update() method purges all event listeners on the descendants of the called element. In your example because the span is a descendant of $('test') the 2nd update purges all the content in $('test') to make sure there are no memory leaks or dangling event listeners. I believe that checking if the current content is exactly equal to the new content then fail silently would be the best route.

savetheclocktower commented 9 years ago

Marking this as an enhancement just because I have no idea what we should do in this scenario.