6 Replies Latest reply on Jul 14, 2008 4:39 AM by the_cthulhu

    invoke action after completion of page load

    the_cthulhu

      hi there!
      I'm having some problems with the following scenario:
      There's a backing bean that generates the data that should be displayed in some DataGrid. As this computation takes some time and blocks the page from beeing loaded I tried to display some ...loading... info and after completion of the message replace it with the datatable.
      At the moment i'm doing this using the following code:

      <a4j:poll
      id="userRecommendationPoll" interval="1"
      action="#{userRecBox.evaluate}"
      enabled="#{userRecBox.resultCount==0}"
      reRender="userRecommendationResult" />

      The evaluate action computes the data for the DataGrid with id userRecommendationResult. The method doesn't return the data but some boolean without purpose to complete the ajax-request-cyclw.
      Is there a better way to do this, as using a4j:poll tends to be problematic if there are no results after completion. Optimal solution would be something like
      <a4j:invoke action="#{userRecBox.evaluate}" reRender="userRecommendationResult" />

      that rerenders the DataGrid after result of evaluate is returned.
      Regards,
      Florian

        • 1. Re: invoke action after completion of page load
          daniel.soneira

          You could try a combination of jQuery (onReady) calling an a4j:jsFunction that triggers the loading of the data - with an aj4:status onstart showing some "loading" div - hiding it in onstop again.

          So your page would show up - when the DOM is built (rich:jQuery timing="onload") you invoke your data loading function (a4j:jsFunction) as an AJAX request - while that function is working in the background you display some status information (a4j:status onstart / onstop)

          • 2. Re: invoke action after completion of page load
            the_cthulhu

            that sounds like a more elegant approach without the possbility of being DDOSed by the users because of the poll not beeing disabled.
            I tried the following:

            <rich:jQuery timing="onload" query="eval()"></rich:jQuery>
             <h:form>
             <a4j:jsFunction name="eval" action="#{userRecBox.evaluate()}" reRender="userRecommendationResult">
             </a4j:jsFunction>
             </h:form>

            this leads to the strange error:
            An Error Occurred:
            Property 'evaluate' not found on type de.nightcrawler.session.box.userRecommendation.UserRecBox_$$_javassist_3

            My first thought was that the action is not called properly but some property. I created a delegate Method "getEvaluate()" in userRecBox but this changes nothing.
            Some idea?


            • 3. Re: invoke action after completion of page load
              the_cthulhu

              problem solved!
              sorry, I tinkered with the old poll code and jsut commented it out, but it was still parsed I think ;)

              • 4. Re: invoke action after completion of page load
                the_cthulhu

                very strange behviour ;)
                So, the above code works perfectly, but only für the jsFunction eval. This overrides the javascript eval function that is called in the generated jQuery:

                jQuery(document).ready(function() {
                var selector = 'page';
                 try {
                selector = eval("");
                 } catch (e) {}
                jQuery(selector).eval();

                So the actual method invocation is done in line 4 and not in the last line.
                Any clues how to solve this problem?

                • 5. Re: invoke action after completion of page load
                  daniel.soneira

                  Naming a JS function "eval" is somewhat dangerous, as it is already a function standard Javascript function.

                  I would go for another name [I'll use "youreval" in the example below]

                  The problem here is that you didn't use a jQuery API function name (like "each", "toggle", ...) in the "query" argument.

                  Try something like this:

                  <rich:jQuery selector="document" timing="onload" query="each(function () {youreval();})"/>


                  This will invoke the inline callback function for every element that matches your selector (which should be your document - so only invoked once).

                  • 6. Re: invoke action after completion of page load
                    the_cthulhu

                    Great, Thank you!
                    I wasn't accustomed to jQuery so I didn't have this great idea.
                    Works fine, thank you very much!