Ok, so my little del.icio.us app (click link to read about how I added Ajax functionality to a simple Rails app) is pretty cool, but it was missing one big thing. When the user clicks the "Get Results" link she has no idea that the page is communicating with the server. While the communication doesn't go on forever, an impatient user might click the link multiple times.

So, we need a way of letting the user know that something is happening and that they ought to wait a bit. Today, the standard seems to be an animated spinning icon:
This means we're waiting for a return back from the server: Spinner

With the help of Brian Eng from Softies on Rails and the insanely awesome prototype javascript library, I've added this functionality to my little app. First, I added this code onto the same line as my Get Results link:


I also changed the Get Results link code from:
1
2
3
4

<%= link_to_remote("Get Results", :update => 'query_results', 
          :with => "'tags=' + $F('query_tags')", 
          :url => { :action => :get_results }) %>
to:
1
2
3
4
5
6
7

<%= link_to_remote("Get Results",
          :update => 'query_results',
          :with => "'tags=' + $F('query_tags')",
          :loading => "Element.show('getting_results')",
          :complete => "Element.hide('getting_results')",
          :url => { :action => :get_results }) %>
Notice that I gave the span the id: "getting_results" and I used style="display:none" to hide it. I toggle it's visibility using the :loading and :complete options of the link_to_remote method. Both the :loading and :complete options specify callbacks, which means that when the server is in such a state (loading - running the method, or complete - method is done), the methods indicated by the options will be called.

This means that the server will announce that it's performing the "get_results" action by calling back to the "Element.show" method set up in the :loading option. The :complete method works similarly.

Ok, so what's Element.show? Element is an object in the prototype.js javascript library which let's you manipulate stuff in the DOM. So, when the server is performing the "get_results" action, it is instructed to call the "Elements.show" prototype method causing it to show the element with the id of "getting_results." AND when the server is done with that, it calls "Elements.hide" on the same element.

To see this sort of thing in action, leave me a comment below. When you submit the comment you'll see the little spinner while the comment is being saved to the database, and then, magically, your comment will appear just above where you wrote it, without having to completely refresh the page.

UPDATE: Be sure to also check out my scriptaclous tutorial as well.

UPDATE2: I just added a video demo of my del.icio.us application showing both the ajax progress indicator as well as some scriptaculous effects. Check it out!

UPDATE3: Since moving this blog to Mephisto, my comments no longer exhibit this behavior, so testing here won't work for you.

129 Responses to “HowTo add Ajax in-progress indicators”

  1. ZZTop Says:
    Get Down.
  2. ap Says:
    Cool!
  3. Brian Eng Says:
    Looks like it's coming along nicely... You can even create other cool visual effects using the Scriptaculous helpers. So you could do :loading => "new Effect.Appear('getting_results')" and that would make your progress indicator fade in. (Gotta love it!)
  4. TAD Says:
    Dang, Brian, you're like the idea fairy or something. :D The fade in thing is pretty darned cool. I think I'm going to play with my app now and see if I can make the results text area fade in or better yet, slide in once we get results. Kinda silly in this context, but a fun way to learn. And then, of course, yet another article.
  5. Rich Says:
    Good tutorial
  6. Eric Says:
    nice
  7. Sid Says:
    Lets see this work.
  8. billy bob Says:
    wow... nice... ok. bye
  9. billy bob Says:
    test 2
  10. test Says:
    test
  11. techno Says:
    test comment
  12. mikey Says:
    testin
  13. nobodyanywhere Says:
    This is extremely helpful
  14. test@test.com Says:
    test
  15. test@test.com Says:
    test
  16. JFSIII Says:
    sorry for the comment spam. I just wanna see it
  17. Matio Says:
    suspicious ;-)
  18. beavis Says:
    Lemme see!
  19. wes Says:
    neato
  20. Charles Bogle Says:
    That's one way to increas the number of comments.
  21. jj Says:
    what about a non-Rails solution? (please)
  22. sdasd Says:
    asdasd
  23. Scott Halstvedt Says:
    Neato, always appreciate things like this.
  24. Sam Says:
    Aww.. rails only?
  25. Mikey Says:
    Ummm.. test? I feel a little silly leaving a non-post... Hmmm...
  26. xcvxcv Says:
    xvxcvxcv
  27. captain lou albano Says:
    You need to add a link for users to delete their bogus followups. Like this one. - Cap'n Lou
  28. steve Says:
    this is very cool. nice tutorial
  29. bry Says:
    noice
  30. George Gonsalves Says:
    Testing it
  31. George Gonsalves Says:
    Antoher long Test
  32. fred Says:
    cool
  33. remy Says:
    Nice
  34. Bob Says:
    Like dewd, this rox!
  35. Bob Says:
  36. Bob Says:
    wowsers!
  37. Bob Says:
    not bad!
  38. csoki Says:
    unbelievable
  39. TheSavior Says:
    wha....
  40. ssxsxsx Says:
    csdca
  41. IT Says:
    Lets see
  42. emo Says:
    testing it
  43. Chris Says:
    Very nice. Thanks.
  44. Xeno Says:
    test
  45. Boz75 Says:
    Cool man
  46. This is my name Says:
    This is my message
  47. My Name Says:
    message
  48. test Says:
    test
  49. test Says:
    test
  50. test Says:
    test
  51. me Says:
    testing
  52. test Says:
    test
  53. so another test Says:
    let's see
  54. me Says:
    55
  55. AlaaEddin Says:
    cooooooool
  56. AlaaEddin Says:
    Cool Ajax
  57. Gert-Jan Says:
    Ah finally someone who posts the loading image.. Nice article
  58. yo mama Says:
    nice work
  59. see it Says:
    here is my message
  60. see it Says:
    tretretret
  61. Zhang-Zi Says:
    I'm just looking for this thing, thank you
  62. Test Says:
    Cool!
  63. asegfr Says:
    sadgadfg
  64. dopamin Says:
    Thanks once again for inspiration to Brian Eng of Softies on Rails, here's my latest tutorial. Brian mentioned how easy it would be to add script.aculo.us effects to my little delicious app. And he was totally correct. What are script.aculo.us eff...
  65. John Ripper Says:
    I test all of you
  66. Redacted Says:
    Sounds useless.
  67. Redacted Says:
    yep, sure is.
  68. jdbrasco Says:
    I'll like to see it in action
  69. Harry Says:
    hello
  70. Ryan Says:
    In soviet russia, comments post YOU!
  71. Checking Says:
    Just checking...
  72. testing Says:
    testing
  73. testing Says:
    testing
  74. brad Says:
    let's have a look
  75. Ich Says:
    Nicht schlecht!
  76. tim phang Says:
    Haha, i love how many comments you recieved from people wanting to see it in action...
  77. test Says:
    test
  78. test Says:
    zzzzzzzzzz
  79. test Says:
    zzzdasf
  80. test Says:
    sdfaaefae
  81. hello Says:
    hello
  82. hello Says:
    hihi
  83. hello Says:
    hihi
  84. S Says:
    test
  85. test Says:
    test
  86. www.ntsc.com Says:
    kewl stuff
  87. www.ntsc.com Says:
    xzZXCzX
  88. detz Says:
    The down fall is it doesn't refresh, I see this on forums now when I post something on a busy thread someone might have posted before me but when I submit the new message does not show up so I have to refresh anyways.
  89. me Says:
    cool
  90. test Says:
    test
  91. Francis Says:
    Hello thanks for this thing...cool!!
  92. Francis Says:
    test
  93. tester Says:
    your app is kewl!
  94. tester Says:
    this is a test
  95. tester Says:
    this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test this is a test
  96. Tree Says:
    Very nice, thanks a lot
  97. Joe Says:
    Boom shaaka laka
  98. c Says:
    cxcx
  99. xcz Says:
    cxzcxz
  100. Pete Says:
    Wow that's a bunch 'o comment!
  101. tesa Says:
    well love this
  102. ds Says:
    sd
  103. df Says:
    df
  104. eric Says:
    test
  105. SMR Says:
    coool.
  106. test Says:
    test
  107. cd Says:
    cd
  108. cd Says:
    td
  109. aa Says:
    aaa
  110. checking Says:
    Good job :)
  111. lemmaseee Says:
    ahahaha
  112. john Says:
    im interested in seeing this!
  113. The Fool Says:
    Just testing this out. You can remove the comment.
  114. The Fool Says:
    lalala
  115. The Fool Says:
    awefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawef
  116. hgfh Says:
    hgfhf
  117. TicTacToe Says:
    And a ToeTacTic
  118. TicTacToe Says:
    ToTikTak
  119. TicTacToe Says:
    TicTic
  120. TicTacToe Says:
    TacTac
  121. TicTacToe Says:
    ToeToe
  122. defect Says:
    some message here
  123. microflyer Says:
    cool
  124. k Says:
    d
  125. abishek.s@tcs.com Says:
    too good yaar
  126. peter Says:

    tescht

  127. sepp Says:

    tets

  128. nnssde Says:

    gfd

  129. test Says:

    asadf

Sorry, comments are closed for this article.