Following typical REST standards, I have broken down my resources into separate endpoints and calls. There are two main objects: List and Item (and, of course, the list has a list of elements, as well as some other data associated with it).
Therefore, if the user wants to get his lists, he can make a Get request on api/Lists
Then the user may want to get items in one of these lists and do Get to api/ListItems/4 , where 4 was found from List.listId retrieved in the previous call.
This is good and good: the options.complete $.ajax allows me to point to a callback method, so I can optimize these two events.
But everything becomes very dirty if I want to get items for all the lists in question. For example, suppose I have a library function called makeGetRequest that accepts an endpoint and callback function to make this code cleaner. Simple extraction of 3 elements in a naive way leads to the following:
var success1 = function(elements){ var success2 = function(elements){ makeGetRequest("api/ListItems/3", finalSuccess); } makeGetRequest("api/ListItems/2", success2); } makeGetRequest("api/ListItems/1", success1);
Disgusting! This is what we talk about in programming 101, we press the wrists and point the loops. But how can you do this with a loop without relying on external storage?
for(var i : values){ makeGetRequest("api/ListItems/" + i, successFunction); } function successFunction(items){
And even with the repository, I would have to know when everyone had finished and retrieved their data, and call some main function that retrieves all the collected data and does something with it.
Is there any practice for this? This must have been allowed many times before ...