2

I have the following working very nicely, loading a partial on click of a link. I'm having trouble however modifying this so that the partial loads on document ready or similar. I basically want to load my slow loading partials asynchronously. Can anyone point me in the right direction please? I feel like it's probably a small modification to make this happen.

#ProductsController
def show_territories  
  respond_to do | format |  
    format.js {render :layout => false}  
  end
end

#products/show.html.erb
<%= link_to 'Show Territories', show_territories_product_path(:id => @product.id),  :remote => true %>
<div id="spinner" class="spinner"><%= image_tag("ajax-loader-2.gif") %></div>
<div id="territories" class="tab-contents"></div>

#products/show_territories.js.erb
$( "#territories" ).html( "<%= escape_javascript( render( :partial => "territories") ) %>" );

#products/_territories.html.erb
<!--- partial view code goes here --->


#custom.js
$(function(){
// hide it first
$("#spinner").hide();

// when an ajax request starts, show spinner
$(document).ajaxStart(function(){
    $("#spinner").show();
});

// when an ajax request complets, hide spinner    
$(document).ajaxStop(function(){
    $("#spinner").hide();
});
}); 
1

2 Answers 2

3

Well you could simply trigger the partials loading manually by calling click() on them.

<%= link_to 'Show Territories', show_territories_product_path(:id => @product.id), :remote => true, :id => 'territories_link' %>

$(function(){
  // hide it first
  $("#spinner").hide();

  $('#territories_link').click();

  ...
});
Sign up to request clarification or add additional context in comments.

3 Comments

That's awesome, works perfectly and I can see via Chrome's RailsPanel that i'm getting two processes loading async. My spinner's stopped working though, but i'll figure that out. Thanks!!
ok! glad I could help : ) Just FYI, the reason why it loads asynchronously is that javascript "clicks" on the links but does not wait for the response to happen and just continues executing the rest of the code.
Yeah it's cool, pretty sure it's exactly what I need. Only thing that concerns me is if my faster loading elements stall for some reason that perhaps loading this before they've finished might hinder their loading. But perhaps not if they're async?
1

I've been trying some stuff out based on the code of rails's jquery-ujs (handleRemote method). It seems this works quite nicely and has no need of putting an actual link in the page:

    $(function() {
        var tmpElement = $(document.createElement('a'))
        tmpElement.data('remote',true)
        tmpElement.prop('href', '<%= j show_territories_product_path(id: @product.id) -%>')
        $.rails.handleRemote(tmpElement)
    })

1 Comment

Great trick! Works great, just have to be careful of Turbolinks with it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.