HowTo add Ajax in-progress indicators
April 24th, 2006
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:
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:
to:
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.
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:
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 }) %> |
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 }) %> |
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”
Sorry, comments are closed for this article.
June 10th, 2007 at 07:21 PM Get Down.
June 10th, 2007 at 07:21 PM Cool!
June 10th, 2007 at 07:21 PM 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!)
June 10th, 2007 at 07:21 PM 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.
June 10th, 2007 at 07:21 PM Good tutorial
June 10th, 2007 at 07:21 PM nice
June 10th, 2007 at 07:21 PM Lets see this work.
June 10th, 2007 at 07:21 PM wow... nice... ok. bye
June 10th, 2007 at 07:21 PM test 2
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM test comment
June 10th, 2007 at 07:21 PM testin
June 10th, 2007 at 07:21 PM This is extremely helpful
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM sorry for the comment spam. I just wanna see it
June 10th, 2007 at 07:21 PM suspicious ;-)
June 10th, 2007 at 07:21 PM Lemme see!
June 10th, 2007 at 07:21 PM neato
June 10th, 2007 at 07:21 PM That's one way to increas the number of comments.
June 10th, 2007 at 07:21 PM what about a non-Rails solution? (please)
June 10th, 2007 at 07:21 PM asdasd
June 10th, 2007 at 07:21 PM Neato, always appreciate things like this.
June 10th, 2007 at 07:21 PM Aww.. rails only?
June 10th, 2007 at 07:21 PM Ummm.. test? I feel a little silly leaving a non-post... Hmmm...
June 10th, 2007 at 07:21 PM xvxcvxcv
June 10th, 2007 at 07:21 PM You need to add a link for users to delete their bogus followups. Like this one. - Cap'n Lou
June 10th, 2007 at 07:21 PM this is very cool. nice tutorial
June 10th, 2007 at 07:21 PM noice
June 10th, 2007 at 07:21 PM Testing it
June 10th, 2007 at 07:21 PM Antoher long Test
June 10th, 2007 at 07:21 PM cool
June 10th, 2007 at 07:21 PM Nice
June 10th, 2007 at 07:21 PM Like dewd, this rox!
June 10th, 2007 at 07:21 PM
June 10th, 2007 at 07:21 PM wowsers!
June 10th, 2007 at 07:21 PM not bad!
June 10th, 2007 at 07:21 PM unbelievable
June 10th, 2007 at 07:21 PM wha....
June 10th, 2007 at 07:21 PM csdca
June 10th, 2007 at 07:21 PM Lets see
June 10th, 2007 at 07:21 PM testing it
June 10th, 2007 at 07:21 PM Very nice. Thanks.
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM Cool man
June 10th, 2007 at 07:21 PM This is my message
June 10th, 2007 at 07:21 PM message
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM testing
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM let's see
June 10th, 2007 at 07:21 PM 55
June 10th, 2007 at 07:21 PM cooooooool
June 10th, 2007 at 07:21 PM Cool Ajax
June 10th, 2007 at 07:21 PM Ah finally someone who posts the loading image.. Nice article
June 10th, 2007 at 07:21 PM nice work
June 10th, 2007 at 07:21 PM here is my message
June 10th, 2007 at 07:21 PM tretretret
June 10th, 2007 at 07:21 PM I'm just looking for this thing, thank you
June 10th, 2007 at 07:21 PM Cool!
June 10th, 2007 at 07:21 PM sadgadfg
June 10th, 2007 at 07:21 PM 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...
June 10th, 2007 at 07:21 PM I test all of you
June 10th, 2007 at 07:21 PM Sounds useless.
June 10th, 2007 at 07:21 PM yep, sure is.
June 10th, 2007 at 07:21 PM I'll like to see it in action
June 10th, 2007 at 07:21 PM hello
June 10th, 2007 at 07:21 PM In soviet russia, comments post YOU!
June 10th, 2007 at 07:21 PM Just checking...
June 10th, 2007 at 07:21 PM testing
June 10th, 2007 at 07:21 PM testing
June 10th, 2007 at 07:21 PM let's have a look
June 10th, 2007 at 07:21 PM Nicht schlecht!
June 10th, 2007 at 07:21 PM Haha, i love how many comments you recieved from people wanting to see it in action...
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM zzzzzzzzzz
June 10th, 2007 at 07:21 PM zzzdasf
June 10th, 2007 at 07:21 PM sdfaaefae
June 10th, 2007 at 07:21 PM hello
June 10th, 2007 at 07:21 PM hihi
June 10th, 2007 at 07:21 PM hihi
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM kewl stuff
June 10th, 2007 at 07:21 PM xzZXCzX
June 10th, 2007 at 07:21 PM 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.
June 10th, 2007 at 07:21 PM cool
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM Hello thanks for this thing...cool!!
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM your app is kewl!
June 10th, 2007 at 07:21 PM this is a test
June 10th, 2007 at 07:21 PM 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
June 10th, 2007 at 07:21 PM Very nice, thanks a lot
June 10th, 2007 at 07:21 PM Boom shaaka laka
June 10th, 2007 at 07:21 PM cxcx
June 10th, 2007 at 07:21 PM cxzcxz
June 10th, 2007 at 07:21 PM Wow that's a bunch 'o comment!
June 10th, 2007 at 07:21 PM well love this
June 10th, 2007 at 07:21 PM sd
June 10th, 2007 at 07:21 PM df
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM coool.
June 10th, 2007 at 07:21 PM test
June 10th, 2007 at 07:21 PM cd
June 10th, 2007 at 07:21 PM td
June 10th, 2007 at 07:21 PM aaa
June 10th, 2007 at 07:21 PM Good job :)
June 10th, 2007 at 07:21 PM ahahaha
June 10th, 2007 at 07:21 PM im interested in seeing this!
June 10th, 2007 at 07:21 PM Just testing this out. You can remove the comment.
June 10th, 2007 at 07:21 PM lalala
June 10th, 2007 at 07:21 PM awefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawefawef
June 10th, 2007 at 07:21 PM hgfhf
June 10th, 2007 at 07:21 PM And a ToeTacTic
June 10th, 2007 at 07:21 PM ToTikTak
June 10th, 2007 at 07:21 PM TicTic
June 10th, 2007 at 07:21 PM TacTac
June 10th, 2007 at 07:21 PM ToeToe
June 10th, 2007 at 07:21 PM some message here
June 10th, 2007 at 07:21 PM cool
June 10th, 2007 at 07:21 PM d
June 10th, 2007 at 07:21 PM too good yaar
July 9th, 2007 at 04:00 AM
tescht
July 9th, 2007 at 04:02 AM
tets
July 9th, 2007 at 01:18 PM
gfd
July 10th, 2007 at 09:13 PM
asadf