Skip to main content
Known Participant
May 28, 2009
Question

Need ajax to make user wait until server has performed task.

  • May 28, 2009
  • 2 replies
  • 1690 views

Admittedly I'm a total noob to ajax AND somewhat even javascript and have in fact been avoiding it, despite having been a CF dev for nearly ten years.  But here I am wondering if someone would be gracious to help point me in the right direction since I can't seem to escape it this time.

Here's the scenario.  This is for an ecommerce site that sells downloads.  Once the purchase clears, the application begins the creation and placement of the purchased files.  The user is given the success page with a link to the download area.  They click into the download area.  Now the creation of their files may or may not yet be complete.  SO what I must do is load the page which then displays *wait, this file is being created* images next to the link of each file until the file is complete.  Then the link becomes active.

I've created my cfc that receives the ID of the record to check and returns its status (1 or 0).

Well, now what?  I know I need to put some code on the page so it can display the wait image, make those calls to the database until it gets a 1 then change the image to a link but ?!?!? I'm at a total dead stop here.  I can't even find any similar apps on the net to work off of.

Can you help me know the steps/tags to use?

I have CF8 Enterprise and own the server/site so have complete control over any all setttings.

TIA!

    This topic has been closed for replies.

    2 replies

    Inspiring
    May 28, 2009

    Thanks for the added info, ValC!

    Okay, this is but a suggestion/thought on how one could approach such a task.

    Note: I use jQuery for my Ajax library but other libraries (MooTools, Prototype, Dojo, etc.) should have similar solutions and work equally well. I simply prefer jQuery.

    Since we're dealing with multiple CFM pages, I might take the following approach:

    1. Hold off creating the files to the success page (if this process is not there already)
    2. Use an Ajax call to the appropriate CFM/CFC, from the success page, to start the file generation process
    3. Add an event listener to this Ajax call
    4. While the call is in progress, show your wait image
    5. When the call reports back, display the link to the download page

    Here's a very basic way jQuery might be used with such an approach.

    Success page's JavaScript:

    <script src='load_jquery_lib.js'></script>

    <script>

      // jQuery fires this function when the DOM is loaded and ready to use/manipulate

      $(document).ready(

         function(){

            // set the path to your 'action' page or CFC

            _url = 'path/to/mycfc.cfc?method=doFileGeneration&returnformat=json';

         // .getJson gets data from the specified URL (above), passing the params (data)

            // via an HTTP GET action, and sets a function (callback) to run when it receives

            // a message back from the server

            $.getJSON(

              _url,           { var1: #some_var#,

                      var2: #some_other_value# },

              doReturnAction      );

            // the following will set an image to display in the div with matching id attribute

            $('#my_div_with_wait_image').html( 'show wait image: <img src="" />' );

          }

      );

      function doReturnAction( data, status ){

         if( data.FILE_CREATED == true ){

              // we're good. code below clears image set above and adds a download link, etc.

              $('#my_div_with_wait_image').html( 'now show download link: <a href=""></a>' );

         }

         else{

              // danger! something went wrong. show error message.

              $('#my_div_with_wait_image').html( 'some error message' );

         }

      }

    </script>

    The cfc method called from the JS above:

    <cffunction name="doFileGeneration" output="true" access="remote" returntype="void">      <cfargument name="var1" required="true" />

            <cfargument name="var2" required="true" />      <cfscript>           results = StructNew();

                    // we'll assume that the generation method returns a boolean           results.file_created = method_to_generate_files( arguments.var1, arguments.var2 );         

              theJSON = SerializeJSON( results );                    </cfscript>      <cfoutput>#theJSON#</cfoutput>          </cffunction>

    This is all just a basic skeleton of one way you could go about handling your tasks. I hope it helps get you started.

    Best,

    Craig

    ValCscAuthor
    Known Participant
    May 28, 2009

    I can't thank you enough for taking the time to help push me along.  I'm already using jquery several times on the site so it's already in my assets.

    I'll have a go at it tomorrow morning (I'm a WAH mom of 4, so it's kiddo time here).

    Inspiring
    May 28, 2009

    No problem! Let us know if you hit a snag trying to implement tomorrow and good luck with kiddo time (wow, 4!!)

    Inspiring
    May 28, 2009

    Hi,

    Welcome to the exciting and often painful world of asynchronous code ! It is worth the pain, though!

    In the OP, you noted that:

    Once the purchase clears, the application begins the creation and placement of the purchased files. The user is given the success page with a link to the download area.

    When the purchase clears, the user being sent to another CFM page (success page), true? Is the download area a part of the success page or a link to another CFM page, from which they download?

    Sorry for the questions, I only ask b/c the Ajax(y) approach I would take would differ based on the application flow from the point of order completion/purchase clearing. We can definitely get you some good steps and ideas but, for me, knowing the flow helps in giving better suggestions and advice.

    ValCscAuthor
    Known Participant
    May 28, 2009
    When the purchase clears, the user being sent to another CFM page (success page), true? Is the download area a part of the success page or a link to another CFM page, from which they download?


    Of course I can do it whichever way I want but I've planned so far...

    The download is another cfm page (globally available for use from both user profile/previous orders access AND checkout success).  I've made it a URL link from the success page.  The files can take anywhere from 10 - 30 seconds to generate so I figure this buys me some time for user to get oriented and click.

    Thanks for the interest and help.