Seems you have two options:
1) Cache the AJAX calls
2) Hide the already retrieved data
It sounds like you are implementing an AJAX tree to me (since you mentioned
folders...). Either way these ideas might help:
Why not just wrap the children elements in a DIV and set the display style
attribute to "none". Attach an exando ( You can assign random
attributes to html elements via JS and refer to them at a later date i.e.
someHtmlElement.myAttribute = someOtherValue ) attribute to the activating
element to reference the containing hidden child. Then on your click handler,
check the expando attribute for a non-null value, if it''s not null,
assume it''s the child elements containing DIV and just show it. No
id''s needed =D
The other way is to use an object as a hash with a key of sorts. If you are
using a GET request for your HTML, then you can use the URL as the key. If you
are using post, the post data (or a substring of the post if there is a variable
part of it that might change between requests.) You then need to wrap your ajax
request with another call that checks the cache for a value, if the value is
found, activate your success call back function with a fake request object that
fills out the responseText.
-Andrew Martinez
-----Original Message-----
From: rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-spinoffs@googlegroups.com]On Behalf Of Tobias Numiranta
Sent: Wednesday, September 13, 2006 9:53 AM
To: Ruby on Rails: Spinoffs
Subject: [Rails-spinoffs] Re: client side caching for ajax.
Hi again. this is how I solved it:
-- helper code:
# support for client side cached requests
def include_cache_request_script
%Q{<script> function cached_request(remote_func, update) {
if($(update).innerHTML == "") { remote_func(); }
else { Element.toggle(update); }
} </script>}
end
def cached_link_to_remote(title, options)
func = "cached_request(function() {#{remote_function(options)};},
''#{options[:update]}'')"
link_to_function title, func
end
-- view code:
<%= include_cache_request_script %>
...
...
<%= cached_link_to_remote title, :update => doc_id,
:url => {:action =>
''foo'', :id =>
item_id } %>
What do you think about it? Have a nice day,
T
Tobias Numiranta wrote:> Hi. new prototype rails user says:
>
> Is there any convenient way to cache results on the client-side? I have
> some code with a file browser, where expanding a folder performs a
> ajax-call. It would be nice, when collapsing the tree to store the
> expanded result somehow.
>
> So that the result from calling something like this,
>
> <%= link_to_remote anode.title, :update =>
"node_li#{anode.id}",
> :url => {:action => "atree", :id =>
anode.id} %>
>
> can be reused without a http-request the second time.
>
> Have a nice day,
> T
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs
-~----------~----~----~----~------~----~------~--~---