coffeescript - Consistent ordering of Ajax results -


i have coffeescript code running in playframework builds table based on several ajax queries.

$ ->     $.get "/queues", (queues) ->         $.each queues, (index, queue) ->             $.get "/queue/" + queue, (html) ->                 $("#queues").append $("<tr>").html html 

/queues returns list of queue names.
/queue/name returns html describing queue name.
#queues table, display description.

this works fine, since queries asynchronous, append calls executed in different order each time, means table contents randomly reordered every time query runs. since providing current status of dynamic, query runs every 3 seconds.

what way have rows displayed in consistent order every time? have index, try adding data row, if third query returns first, adding row 3 not going work well.

i not have strong preference how ordering done, consistent on multiple loads.

one option force queries sequential. example, define recursive function results of 1 query before making next one:

do_queue = (index, queues) ->     if index >= queues.length return     queue = queues[index]     $.get "/queue/" + queue, (html) ->         $("#queues").append $("<tr>").html html         do_queue(index+1, queue) 

then call this:

$ ->     $.get "/queues", (queues) ->         do_queue(0, queues) 

Comments

Popular posts from this blog

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -