Prevent Rails link_to_remote multiple submits w Javascript

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-12-23T05:28:32Z Indexed on 2010/12/23 15:54 UTC
Read the original article Hit count: 308

In a Rails project I need to keep a link_to_remote from getting double-clicked. It looks like :before and :after are my only choices - they get prepended/appended to the onclick Ajax call, respectively. But if I try something like:

:before => "self.stopObserving()"

t,he Ajax is never run. If I try it for :after the Ajax is run but the link never stops observing.

The solutions I've seen rely on creating a variable and blocking the whole form, but there are multiple link_to_remote rows on this page and it is valid to click more than one of them at a time - just not the same one twice. One variable per row declared outside of link_to_remote seems very kludgey...

Instead of using Prototype I originally tried plain Javascript first for this proof of concept - but it fails too:

<a href="#" onclick="self.onclick = function(){alert('foo');};">click</a>

just puts up an alert when clicked - the lambda here does nothing? This next one is more like the desired goal and should only alert the first time. But instead it alerts every time:

<a href="#" onclick="alert('bar'); self.onclick = function(){return false;};">click</a>

All ideas appreciated!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about ruby-on-rails