5 Replies Latest reply on Jul 6, 2009 9:44 AM by Paul Cook

    A4J.AJAX.AddListener resetting after ajax request?

    Paul Cook Newbie

      Headache inducing scenario involving Magnolia cms v4.0.1, jQuery v1.3.2, Seam v2.1.1, RichFaces v3.3.1, jboss 4.2.2, Firefox 3.0.11/IE6.0...

      - Main page is served up from Magnolia
      - Its document-ready uses jQuery.load() to ajax in a form from Seam
      - The seam form is using a4j: taglib for ajax validation & rerendering of form elements

      That all works fine. Phew. :)

      For consistency across the whole site we replaced rich:calendar in the seam fragments with jquery.datepicker thats used on all the other (non-seam/rich) pages. As a minor consequence, each time an a4j rerender fired, the text controls would lose their jquery datepicker bindings - but no big problem we thought - we can tag a listener into A4J.AJAX.AddListener() and hook them into jquery again after each ajax call.

      And thats where the headaches began. :( No matter whether we initially hook up the global ajax listener in the main magnolia page or in the injected seam fragment, they're not surviving >1 ajax rerender call.

      For example, with this code in doc-ready in the main magnolia page ...

      jQuery(document).ready(function() {

      onafterajax: function(req,evt,data) { alert("onafterajax "+A4J.AJAX._listeners.length);}

      alert("ready "+A4J.AJAX._listeners.length);

      jQuery('.ajaxMe').each(function() {
      var ajaxMe = jQuery(this);
      ajaxMe.load(contextPath + "/" + ajaxMe.attr("link"));


      The initial "ready" alert will popup and report 1 listener. All ok. jquery drags in the seam fragment. All ok. When the seam fragment does its first a4j rerender call to validate a field, theres no "onafterajax" popup - the listener is gone. I added an onbefore listener too, that didnt popup. So with the listener registered in the main magnolia page, its gone after the seam fragment loads.

      Slighty different results if the AddListener + alert is moved into an inline JS block within the seam fragment. The first a4j rerender does end with a popup reporting 1 listener. All ok. But then the listener is gone by the time the next rerender validation occurs.

      I've grepped all the js packs that are being dragged in to the page (tracked them thro firebug).

      There arent any calls to A4J.AJAX.removeListeners() lurking in any of them. Its almost as if the A4J object is cleaned out (or maybe discarded and recreated?) on each rerender?? Is it being overwritten somehow in this weird double ajax aggregation scenario?

      Any wise words would be gratefully received. Even unwise words would be considered ;)