<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code># Behave like +link_to_remote+, but shows a spinner while the request is processing.
#   link_to_remote_with_spinner 'Hi', :url =&gt; hi_path
# You can specify the spinner and the container to hide
#   link_to_remote_with_spinner 'Hi', :url =&gt; hi_path, :container_id =&gt; 'container', :spinner =&gt; 'spinner'
def link_to_remote_with_spinner(title, options)
  element_id = options.delete(:id) || ('link_to_' + title.underscore.tr(' ', '_'))
  container_id = options.delete(:container_id) || element_id
  
  returning '' do |out|
    unless spinner = options.delete(:spinner)
      spinner = "#{element_id}_spinner"
      out &lt;&lt; image_tag('spinner.gif', :id =&gt; spinner, :style =&gt; 'display:none')
    end
    options[:complete] = "$('#{spinner}').hide(); " + (options[:complete] || "$('#{container_id}').show()")
    
    out &lt;&lt; link_to_remote(title, { :loading =&gt; "$('#{container_id}').hide(); $('#{spinner}').show()" }.merge(options),
                                 { :id =&gt; element_id })
  end
end</code>
  <comment>We use this utility internally. I had a use case today where I don't want to $('#{container_id}').hide(); rather I'd like to grey it out and stop it from receiving events. Is there a more generic way to do all this?</comment>
  <created-at type="datetime">2007-09-20T14:17:41+00:00</created-at>
  <id type="integer">13</id>
  <language>Ruby</language>
  <permalink>link_to_remote_with_spinner</permalink>
  <refactors-count type="integer">4</refactors-count>
  <title>link_to_remote_with_spinner</title>
  <trackback-url></trackback-url>
  <updated-at type="datetime">2007-09-24T20:36:21+00:00</updated-at>
  <user-id type="integer">3</user-id>
  <refactors type="array">
    <refactor>
      <code>def link_to_remote_with_spinner(title, options)
  element_id = options.delete(:id) || ('link_to_' + title.underscore.tr(' ', '_'))
  container_id = options.delete(:container_id) || element_id
  
  returning '' do |out|
    unless spinner = options.delete(:spinner)
      spinner = "#{element_id}_spinner"
      out &lt;&lt; image_tag('spinner.gif', :id =&gt; spinner, :style =&gt; 'display:none')
    end
    options[:complete] = "$('#{spinner}').hide(); " + (options[:complete] || "$('#{container_id}').show()")
    options[:loading] = "$('#{spinner}').show(); " + (options[:loading] || "$('#{container_id}').hide()")
    
    out &lt;&lt; link_to_remote(title, options, { :id =&gt; element_id })
  end
end

# Now if you specify complete and loading you can do wathever you want:
link_to_remote_with_spinner 'Grey out text', :url =&gt; ouch_path, :id =&gt; 'my_link',
                            :loading =&gt; toggle = "$('my_link').toggleClassName('disabled')",
                            :complete =&gt; toggle
                            
link_to_remote_with_spinner 'Dont click!', :url =&gt; ouch_path,
                            :loading =&gt; "$('notice').update('I told you not to click')",
                            :complete =&gt; "$('notice').update('Ok now! Look what you did, stupid!')"
</code>
      <code-id type="integer">13</code-id>
      <comment>With the above code you could already specify the onComplete callback overriding the container hiding thing, I just added the same thing for the loading callback and now you got more freedom then a jobless freelancer on crack.</comment>
      <created-at type="datetime">2007-09-21T07:42:00+00:00</created-at>
      <id type="integer">32</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On link_to_remote_with_spinner</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>&lt;%= link_to_remote ... %&gt; &lt;%= indicator %&gt;</code>
      <code-id type="integer">13</code-id>
      <comment>Use my plugin at http://agilewebdevelopment.com/plugins/remote_helpers

See the readme for info on more advanced usage... or email me.</comment>
      <created-at type="datetime">2007-09-27T23:51:47+00:00</created-at>
      <id type="integer">60</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On link_to_remote_with_spinner</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Andrew Kaspick</user-name>
      <user-website>www.redlinesoftware.com</user-website>
    </refactor>
    <refactor>
      <code>&lt;%= link_to_remote 'Text', :url =&gt; your_url %&gt; &lt;%= indicator %&gt;</code>
      <code-id type="integer">13</code-id>
      <comment>Using my plugin remote_helpers from http://agilewebdevelopment.com/plugins/remote_helpers, all of the spinner functionality is taken care of.  It also works with any method that makes a remote function call.

See the plugin README in the plugin for more detailed usage.

The code below is the most basic usage.  The 'indicator' method contains the spinner image and that's all there is to it... the spinner automatically gets displayed and hidden.</comment>
      <created-at type="datetime">2007-09-28T01:33:11+00:00</created-at>
      <id type="integer">77</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On link_to_remote_with_spinner</title>
      <user-id type="integer">31</user-id>
      <user-name>Andrew Kaspick</user-name>
      <user-website>http://www.redlinesoftware.com</user-website>
    </refactor>
    <refactor>
      <code># include the eventselectors.js library in your layout
# put this at the very bottom:

Ajax.Responders.register({
  onComplete: function() { Element.hide('loading'); EventSelectors.assign(Rules);},
  onLoading: function() { Element.show('loading');}
})</code>
      <code-id type="integer">13</code-id>
      <comment>Anymore, I always use EventSelectors.js to handle all toggling of the "loading" image. I usually keep the loading image in a nice place in the layout (often absolutely positioned) and show activity that way.

References:

1) &lt;a href="http://rpheath.com/posts/2007/03/17/add_a_loading_image_for_all_ajax_requests"&gt;loading image for all ajax requests&lt;/a&gt;
2) &lt;a href="http://rpheath.com/posts/2007/06/01/rails-plugin-for-handling-ajax-spinners"&gt;Rails plugin to show loading image&lt;/a&gt;</comment>
      <created-at type="datetime">2007-10-08T23:04:19+00:00</created-at>
      <id type="integer">335</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On link_to_remote_with_spinner</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Ryan</user-name>
      <user-website>http://rpheath.com</user-website>
    </refactor>
  </refactors>
</code>
