Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The biggest thing you can watch out for is events, and how you assign them.</p> <p>For instance, take this scenario (since you haven't provided one):</p> <pre><code>&lt;div id="ajaxResponseTarget"&gt; ... &lt;/div&gt; &lt;script type="text/javascript"&gt; $(someButton).observe('click', function() { new Ajax.Updater($('ajaxResponseTarget'), someUrl, { onSuccess: function() { $$('#ajaxResponseTarget .someButtonClass').invoke('observe', 'click', function() { ... }); } }); }); &lt;/script&gt; </code></pre> <p>This will create a memory leak, because when <code>#ajaxResponseTarget</code> is updated (internally, Prototype will use <code>innerHTML</code>) elements with <code>click</code> events will be removed from the document without their events being removed. The second time you click <code>someButton</code>, you will then have twice as many event handlers, and garbage collection can't remove the first set.</p> <p>A way to avoid this is to use event delegation:</p> <pre><code>&lt;div id="ajaxResponseTarget"&gt; ... &lt;/div&gt; &lt;script type="text/javascript"&gt; $('ajaxResponseTarget').observe('click', function(e) { if(e.element().match('.someButtonClass')) { ... } }); $(someButton).observe('click', function() { new Ajax.Updater($('ajaxResponseTarget'), someUrl); }); &lt;/script&gt; </code></pre> <p>Because of the way DOM events work, the "click" on <code>.someButtonClass</code> will fire also on <code>#ajaxResponseTarget</code>, and Prototype makes it dead simple to determine what element was the target of the event. No events are assigned to elements <em>within</em> <code>#ajaxResponseTarget</code>, so there is no way for replacing its contents to orphan events from targets within.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload