2

I'm using a REST server to retrieve data. In the AngularJS UI the user is given the choice of a few options to create a query to send to the REST server. The problem is the server only accepts one of each, so if user wants to search for multiple Entities, they can't. I'm trying to think of a way to send off multiple requests (quantity of requests depends on length of Entity array, which is set by the user in UI). So far all I can think of is for loop by the length of the entity array, and for each loop send a request - my problem is how do I join these two sets of data? Each time the for loop completes the data is overridden with the next set it's sent off for. And the amount of times the requests are sent is totally dependent on the amount of entities the user needs returned.

5
  • 2
    You have the most typical issue. You can resolver with Javascript closures. Commented Nov 23, 2015 at 11:56
  • As per as I understand that, you are trying to filter some data based on some options. Suppose you have total 100 records and looking for type A,B,C record only. But server returns A records (10) item. Then in next request server returns B type (15) item. and for C (5) item. So you want to display (10+15+5-Common Elements) right? Commented Nov 23, 2015 at 11:58
  • Yes @ParthaSarathiGhosh , but the amount of times request is being sent is based on how many entities user chooses, could be 2 requests, could be 10. Commented Nov 23, 2015 at 12:00
  • Thank you @AitorAznarÁlvarez I will look into closures. Commented Nov 23, 2015 at 12:01
  • Try async.each. Commented Nov 23, 2015 at 12:01

1 Answer 1

1

If you have any unique identifier for each result item then you can try the following algorithm. Hopefully It will solve the issue.

var data = []; 
loop through options selected by user {
   request sent {
       on sucess() {
             loop though RESPONSE_DATA_ARRAY {
                 var id = RESPONSE_DATA_ARRAY_ITEM.uniuqe_key
                 if(data[id] === undefined){
                     data[id] = RESPONSE_DATA_ARRAY_ITEM; 
                     //Stored as Key Value pair, which will help to identify same object each time easily.
                 }
             }

       }
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I will try to implement this and get back :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.