On Oct 29, 2010, at 11:40 AM, RobinBrouwer wrote:
> Hello fellow Rails programmers,
>
> I really love the new way of AJAX requests in Rails3. It really cleans
> up my HTML code and it makes using AJAX in your application a lot
> easier. I only have one - kinda big - problem with it. When the page
> is still loading and the user clicks an AJAX link, the AJAX call will
> not be executed and he/she will be redirected to another page. That's
> because the DOM needs to be loaded before all the links get the AJAX
> functions attached to them. I know I should also add HTML callbacks
> for these links, but for some AJAX links I don't want the user to see
> another page: I want them to see my fancy AJAX and nothing else. I
> didn't have this problem when using link_to_remote and wonder if
> somebody here knows a solution when using UJS.
>
> I tried adding onclick="return false;" to the links, but the normal
> page was still loaded. I asked around for some solutions, but the best
> one I got was adding some <div> over the whole page so all links can't
> be clicked. Then after the DOM is loaded the <div> can be removed. IMO
> that's even worse when it comes to usability, so I wondered if you
> people know a fancy solution for my problem.
>
> To sum up my question: I want my AJAX links not clickable until the
> DOM is loaded. When the DOM is loaded I want them to have the standard
> AJAX functionality they already have. Any suggestions?
If your UJS is wired to the dom:loaded event (Prototype) or the jQuery
equivalent, then everything should work even before the page is
visible in the browser. How are you wiring these links? The other way
to do this is by putting all your script at the bottom of the page,
but that can definitely lead to what you describe -- particularly on
very heavy pages, where the browser will try to stagger the loading.
//load prototype first, then
<script type="text/javascript">
document.observe('dom:loaded',function(){
$('foo').observe('click',function(evt){
evt.stop(); //cancel the event
//do something wizzy here
});
});
</script>
...bunch of html
<a id="foo" href="/bar">baz</a>
The dom:loaded stuff gets instantiated in most cases before the
browser even gets around to displaying things on the page.
Walter
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment