<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Out of my mind...: Better-than-Google activity indicators with Rails</title>
    <link>http://blog.fredjean.net/articles/2006/02/20/better-than-google-activity-indicators-with-rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Frederic Jean's Random Thoughts</description>
    <item>
      <title>Better-than-Google activity indicators with Rails</title>
      <description>&lt;p&gt;
The next time that you use your GMail account, look on the upper-right corner of the screen when you go from email to email. You'll notice that a red box shows up containing the word "Loading...".  This is a very helpful indicator that GMail is in the process of doing something.
&lt;/p&gt;&lt;p&gt;
&lt;a href="http://mir.aculo.us/"&gt;Mir.aculo.us&lt;/a&gt; has &lt;a href="http://mir.aculo.us/articles/2005/11/14/ajax-activity-indicators-with-rails-0-14-3"&gt;a great article&lt;/a&gt; on how to achieve a similar effect using an animated GIF image. You can achieve an even nicer effect by adding partials and &lt;a href="http://script.aculo.us/"&gt;Script.aculo.us&lt;/a&gt; effects.
&lt;/p&gt;&lt;p&gt;
First, make sure that the javascripts files are loaded by adding this to the head of your page:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;table class="typocode_linenumber"&gt;&lt;tr&gt;&lt;td class="lineno"&gt;
&lt;pre&gt;
1
&lt;/pre&gt;
&lt;/td&gt;&lt;td width="100%"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="punct"&gt;&amp;lt;%=&lt;/span&gt;&lt;span class="string"&gt; javascript_include_tag :defaults %&amp;gt;&lt;/span&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;
This will force the default sets of javascripts files to load. These do include effects.js and application.js.
&lt;/p&gt;&lt;p&gt;
Next, create a file called application.js under public/javascripts if it doesn't already exists. Add this code:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;table class="typocode_linenumber"&gt;&lt;tr&gt;&lt;td class="lineno"&gt;
&lt;pre&gt;
1
2
3
4
5
6
7
8
9
10
11
&lt;/pre&gt;
&lt;/td&gt;&lt;td width="100%"&gt;&lt;pre&gt;&lt;code class="typocode_javascript "&gt;&lt;notextile&gt;Ajax.Responders.register({
    onCreate: function() {
        if($('loading') &amp;amp;#38;&amp;amp; Ajax.activeRequestCount&amp;gt;0) {
            Effect.Appear('loading',{duration:0.5});
        }  
    },
    onComplete: function() {
        if($('loading') &amp;amp;&amp;amp; Ajax.activeRequestCount==0)
            Effect.Fade('loading', {duration:1.0});
    }
});&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;
Then create a partial called _loading.rhtml:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;table class="typocode_linenumber"&gt;&lt;tr&gt;&lt;td class="lineno"&gt;
&lt;pre&gt;
1
2
3
&lt;/pre&gt;
&lt;/td&gt;&lt;td width="100%"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;&amp;lt;div id=&amp;quot;loading&amp;quot; style=&amp;quot;display: none;&amp;quot; class=&amp;quot;loading&amp;quot;&amp;gt;
    &amp;lt;h2&amp;gt;&amp;lt;img src=&amp;quot;http://typo.fredjean.net/typo/images/load.gif&amp;quot;&amp;gt;&amp;amp;nbsp;Loading...&amp;amp;nbsp;&amp;lt;/h2&amp;gt;
&amp;lt;/div&amp;gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;
I also added the following CSS to my application's stylesheet:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;table class="typocode_linenumber"&gt;&lt;tr&gt;&lt;td class="lineno"&gt;
&lt;pre&gt;
1
2
3
4
5
6
7
8
&lt;/pre&gt;
&lt;/td&gt;&lt;td width="100%"&gt;&lt;pre&gt;&lt;code class="typocode_css "&gt;&lt;notextile&gt;.loading {
    background: rgb(250,250,250); 
    border: solid 2px; 
    vertical-align: middle; 
    text-align: center; 
    padding-left: 20px; 
    padding-right: 20px;
}&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;p&gt;
I finally included the partial at the bottom of my layout:
&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;table class="typocode_linenumber"&gt;&lt;tr&gt;&lt;td class="lineno"&gt;
&lt;pre&gt;
1
&lt;/pre&gt;
&lt;/td&gt;&lt;td width="100%"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;notextile&gt;&lt;span class="punct"&gt;&amp;lt;%=&lt;/span&gt;&lt;span class="string"&gt; render :partial &lt;/span&gt;&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;loading&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="string"&gt;&lt;/span&gt;&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;
From that point on, I got a "loading" indicator each time I clicked on a link created through the link_to_remote helper method. No need to go ahead and add the effects to each invocations.
&lt;/p&gt;&lt;p&gt;
-- Fred
&lt;/p&gt;





&lt;!-- technorati tags start --&gt;&lt;p style="text-align:right;font-size:10px;"&gt;Technorati Tags: &lt;a href="http://www.technorati.com/tag/Rails" rel="tag"&gt;Rails&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/Ruby" rel="tag"&gt;Ruby&lt;/a&gt;&lt;/p&gt;&lt;!-- technorati tags end --&gt;

</description>
      <pubDate>Mon, 20 Feb 2006 02:46:37 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:14613e940d99e38066ee673b9a1a6e09</guid>
      <author>fred@fredjean.net (Frederic Jean)</author>
      <link>http://blog.fredjean.net/articles/2006/02/20/better-than-google-activity-indicators-with-rails</link>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
