Loading Parts of a Page in the Background
Posted by Craig Ambrose on March 05, 2007 at 10:24 PM
Occasionally I find that one of my apps ends up with “the page from hell”. The design seems to require that a certain page pull in all sorts of information from different places, and perhaps some of it isn’t even viewed straight away, it’s just being loaded into the background so that the user can access it instantly.
I had such a page, which had a growing number of tab pages that I wanted to load without any delay at all. I was starting to get worried about the page load time, but perhaps even worse was the fact that the controller loading this page was becoming quite messy, since some of the tabs clearly involved other resources.
The following helper module might be of some use to you. It defines a helper method called call_remote_queue, which executes a sequence of queued ajax requests.
The Code
module RemoteQueueHelper def call_remote_queue(urls = [], spinner_id = nil)
javascript_tag remote_queue_js(urls.reverse, spinner_id)
endprotected def remote_queue_js(urls, spinner_id)
this_url = urls.pop
this_options = remote_url_options(this_url) return nil unless this_url if urls.empty?
this_options[:complete] = "Element.hide('#{spinner_id}')"
else
this_options[:complete] = remote_queue_js(urls, spinner_id) unless urls.empty?
end
remote_function(this_options)
end def remote_url_options(url)
if url.is_a? Hash
result = url
else
result = {}
result[:url] = url
result[:method] = :get
end
result
endend
Usage
<%= call_remote_queue [user_stories_path(@user), user_dares_path(@user)], 'area_tabbar_spinner' %>
In my case, the urls in question used rjs templates to add the tab page to the list of tab pages. The second, optional parameter is the id of a spinner element to disable when the chain of loading is complete.
Example
This was used on the user profile page of an adult social networking site over at Playful Bent. This is an adult site, so use your own judgement as to whether to view it at work.
Here is a link to a profile which demonstrates this code, and doesn’t display any particularly naughty pictures to annoy your boss (but no guarantees about five minutes from now I’m afraid, it’s user content).

Comments
There are 0 comments on this post. Post yours →
Post a comment
Required fields in bold.