开发者

Rails Rendering a Single Partial after Page Load

开发者 https://www.devze.com 2023-03-28 03:13 出处:网络
I have a partial page in my application named _dashboard.html.erb. Inside of the dashboard, I have a lot of calls to render different partials. One of those is the _calendar.html.erb partial, which ma

I have a partial page in my application named _dashboard.html.erb. Inside of the dashboard, I have a lot of calls to render different partials. One of those is the _calendar.html.erb partial, which makes a call to google calendar and pulls down a list of events. This load takes time and I want to have the _calendar partial load after the rest of the dashboard has loaded. It should be reiterated that I don't want this to be based on a click event and instead when the page loads.

pages_controller.rb

# Logged in to shift
def jump
@title = "#{User.find(session[:user_id]).firstname}'s Dashboard"
@tasks = 开发者_运维问答Task.where(:completed => 0)
@questions = Question.where(:answered => 0)


serv = GCal4Ruby::Service.new
serv.authenticate("email","pass")
cal = GCal4Ruby::Calendar.find(serv, {:title => "Skidmore Center for Sex and Gender Relations"})
#@events = GCal4Ruby::Event.find(serv, {'start-min' => Time.now.utc.xmlschema, 'start-max' => 5.days.from_now.utc.xmlschema })
@events = GCal4Ruby::Event.find(serv, {'start-min' => Time.parse("01/01/2011").utc.xmlschema, 'start-max' => Time.parse("06/01/2011").utc.xmlschema, :calendar => cal.id})

end

Jump is the page where I have the dashboard partial rendering. Here is the part of /app/views/layouts/_dashboard.html.erb that calls the calendar partial:

<div id="main-content">
  <div class="post">
    <h3>calendar</h3>
    <%= render 'layouts/calendar' %>
  </div>
</div>

And /app/views/layouts/_calendar.html.erb

<% @events.each do |event| %>
  <% if event.start_time >= Time.now and event.start_time <= 5.days.from_now %><li><%= event.start_time.to_s(:short) %> <%= event.title %></li><% end %>
<% end %>

I think what you should keep in mind here is that, while I understand how AJAX works, I have little to no experience coding it by itself or in Rails. Any help here would be appreciated (And the more dumbed-down and explained the better!)

Thanks

Update: I used the async_partial gem but am now having issues with the partial pulling the events from google calendar. The partial works without the async_partial.


One of the way to handle this without a gem, which is quite common by the way, is to use some kind of event handler for the page load event. For instance, with jQuery, you can do :

// this get executed when jQuery is made available, which means when 
// your document is ready.
$(function () {

    // you replace the actual calendar content with the piece that is 
    // rendered with your partial
    $("#your-calendar-div-id").replaceWith('<%= escape_javascript(render 'layouts/calendar') %>');

});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号