views:

172

answers:

1

I am currently using link_to_remote in my Ruby on Rails app to render remote content using Prototype. Unfortunately if you click the created link more than once, it re-renders the content.

What I would like to do is somehow disable the link after it is first clicked.

This is the code I am currently using,

<%= link_to_remote (
      "#{user.name}",
      :update => "#{user.id}_div",
      :url => { :controller => "wishlist_items", :action => "load", :id => user.id },
      :position => "after",
      :html => {:class => "#{user.id}_href"} )
  %>

I do know that you can use :success and :complete to call JavaScript, but I am just not sure how to do this.

Any help would be much appreciated. Thank you.

+1  A: 

There are a couple different ways to do this, depending on how you want it to work. If you want the button disabled after a single click, you could probably use one of the callbacks listed in the PrototypeHelper docs:

The callbacks that may be specified are (in order):

  • :loading: Called when the remote document is being loaded with data by the browser.

  • :loaded: Called when the browser has finished loading the remote document.

  • :interactive: Called when the user can interact with the remote document, even though it has not finished loading.

  • :success: Called when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range.

  • :failure: Called when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range.

  • :complete: Called when the XMLHttpRequest is complete (fires after success/failure if they are present).

Basically pick WHEN you want it to be disabled, and use the callback that matches this point to set the onclick attribute on the link to "return false;" or something like that. You could also just delete the element if you don't want it showing up anymore.

If you want the link disabled at the first possible point, what I would do is add an event listener for click events on whatever the ID of the link is, then again set onclick="return false;" or you could delete/hide etc.

Preston Marshall