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

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -