Reputation: 4403
I'm using rails to handle ajax requests for change between menu tabs on my homepage. I tried adding in a new function to hide and show a ajax spinner depending on when ajaxStart and ajaxStop is called, I can't seem to get it working. If this because I'm making ajax calls through rails and shown below? How can I get this going? Thanks a lot for any help.
$(function() {
$('#loading').ajaxStart(function() {
$(this).show();
}).ajaxComplete(function() {
$(this).hide();
});
});
def bookmarks
@bookmarks = ...
respond_with @bookmarks
end
$('#stream').html('<%= escape_javascript(render("posts", :posts => @bookmarks)) %>');
<body>
...
<div id="loading">
<p><img src="images/ajax-loader.gif" /> Please Wait</p>
</div>
</body>
#loading {
display:none;
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
background:black;
}
Upvotes: 1
Views: 1137
Reputation: 16472
I've never used rails so I don't understand/see where your ajax call is happening. That said, your syntax for ajaxStart and ajaxComplete looks fine to me.
Here's a working example using jQuery's typical ajax function (which I assume rails is using under the hood if you're expecting ajaxStart and ajaxComplete to work.
JSFiddle:
HTML
<div id="loading">
<p>Please Wait</p>
</div>
JavaScript
$("#loading").ajaxStart(function() {
$(this).show();
}).ajaxComplete(function() {
var $loading = $(this);
setTimeout(function(){
$loading.hide();
},1000); //Timeout so you can see the loading happen
});
$.ajax({
url: "/echo/json/"
});
CSS
#loading {
display:none;
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
}
Upvotes: 0