View Full Version : Displaying async content in a certain order?

02-02-2009, 10:13 PM
Hi all,

I usually ask really basic questions, but this one is a little more complicated so hopefully one of you AJAX gurus can give me some tips here.

I have an ajax vehicle inventory app. There is a group of collapsible panels, each representing a different group of search criteria. When you click on a panel to open it (I'll use the "Safety" panel as an example), the code looks like this:

this.generateOptions("NHTSA", "NHTSA Safety Rating", pPanel, oGeneratedPanel);
this.generateOptions("Airbags", "Airbags", pPanel, oGeneratedPanel);
this.generateOptions("Emergency", "Emergency Assistance", pPanel, oGeneratedPanel);
this.generateOptions("Safety", "General Safety Features", pPanel, oGeneratedPanel);

(The parameters aren't really important.) Each generateOptions() call boils down to an async request to a web service. Once the server responds to a given call, the callback function creates corresponding markup from the response into a hidden container, then checks to see if the other calls in the group have been completed. Once each call within the group is done, the container is revealed, showing the markup that has been generated with each call.

Everything works great, but since async ajax calls don't always respond in the same order you called them, the order of the various options is constantly changing.

When I originally wrote the app, it was all synchronous, so I didn't have this issue. What do you guys think is the best way to go about controlling the rendering order of the options? Here's some more things that are good to know:

- If none of the vehicles in the database have "Airbag" (just an example) features, no markup gets rendered for that option.
- At any point I can determine which calls have been made, which calls are still in progress, and which have been responded to by the server.

I'm sure I have the info I need to apply some kind of sorting logic to these panels, I just have no idea where to even begin.

Thanks a bunch for any tips.