I'm looking for a good pattern that prevents AJAX requests from overlapping.

Consider this scenario, using an address book application as an example:

1. On the left side of the page, there's a list of contacts. On the right side, 
there's a box that shows the currently selected contact. (Only one contact can 
be open at a time.)
2. The user clicks on a contact to open it.
3. The app initiates an AJAX request, but it takes a while. Maybe the server 
has to do some extra processing for some reason.
4. The user gets impatient, and clicks on another contact before the first one 
has loaded, which initiates a second, parallel AJAX request.
5. The second contact loads immediately, and the user starts working with it, 
but then...
6. ...the first AJAX request finally completes, and the first contact gets 
loaded over the second!

Obviously, the first AJAX request should be canceled if a second request of the 
same type is issued while it's still running.

Here's what I'm thinking: a generalized utility class - say, 
SerialRequestManager - designed to handle all situations of this type. Sort of 
a wrapper around Ajax.Request. A setRequest() method would accept an 
Ajax.Request object (and, if one is already set, destroys the existing one 
first); you'd probably want a cancelRequest() method, as well. The same class 
could hide and show an activity indicator.

To stray from the original problem a bit: a variant class - 
ParallelRequestManager - could add incoming requests to a queue, rather than 
destroying existing ones; it would show the activity indicator when the first 
request was added to the queue, and hide it when the last remaining request in 
the queue completes.

Does any of this sound sensible? :)


      

--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to