-1

I have lists each with checkboxes. Selection on checkbox results in 3 different rest calls and the response is bind with HTML of that list. If user clicks on multiple checkboxes really fast then response of latter calls override the response of previous calls.(Previous call is still playing with response and latter call has overridden the previous's call)

Eg- If I check 6 checkboxes really quick, it might be possible that response might not get executed for some of the rest call.

getRequestRecords: function(obj) {
                return $http({
                    url: $serverPath + '/alumCenter/notifications/request/' +obj',
                    method: "GET"
                });
            },
$scope.singleSelectSet=function(obj){
   myService.getRequestRecords(obj).then(function(response){
       $scope.myVar=response;
          // do lots of things
        });
}

<div ng-repeat="obj in flats track by $index">
<input type="checkbox" class="selectable" ng-click="singleSelectSet(obj)" ng-checked="showChecked($index)" aria-label="{{:: 'common.slideout.aria.slatSelectCheckboxLabel' | i18n }}">
</div>
RahulB
  • 2,070
  • 1
  • 18
  • 26
  • This is probably the same question as this: http://stackoverflow.com/questions/35375120/cancelling-ongoing-angular-http-requests-when-theres-a-new-request – Thomas Ghesquiere Feb 13 '16 at 12:40
  • @ThomasGhesquiere : I dont want to cancel the rest call. It will be wrong in my case. The checkbox is already checked. If its corresponding data is not visible, it will be bad user experience. – RahulB Feb 13 '16 at 13:18
  • We have no idea how `getRequestRecords()` is used. Show all relevant code – charlietfl Feb 13 '16 at 13:28
  • @charlietfl : please see the new edit – RahulB Feb 13 '16 at 13:35
  • What is shown doesn't connect to any checkbox. – charlietfl Feb 13 '16 at 13:38
  • If I understand it correctly, you wish to delay the execution of $http from the next checkbox until the previous call was complete. – Thomas Ghesquiere Feb 13 '16 at 13:39
  • @charlietfl : Does that matter, how rest call is triggered ? I am not sure how that will affect? – RahulB Feb 13 '16 at 13:40
  • @ThomasGhesquiere : Yes. We can say that. – RahulB Feb 13 '16 at 13:40
  • Of course it matters if the triggering is a problem. Doesn't appear any data is sent either so there is a complete disconnect between code shown and problem mentioned in UI. Have voted to close question since code doesn't match problem description – charlietfl Feb 13 '16 at 13:47
  • @charlietfl : The chrome network misses some rest calls. – RahulB Feb 13 '16 at 13:56
  • How are we supposed to help when triggers are unknown? All you are showing is code snippets and not showing how code is used or what it should be doing – charlietfl Feb 13 '16 at 14:01
  • Also appears you have a long history of not accepting answers on any of your questions. That isn't being respectful of how this community works – charlietfl Feb 13 '16 at 14:12
  • @charlietfl : Those answers didn't solved my queries exactly or something near by. If your answer will solve my query. I will mark it accepted – RahulB Feb 13 '16 at 14:17

1 Answers1

0

Is it an option to use an array and push the response onto it? This way you will not overwrite the variable in your $scope.

$scope.myVar = [];

myService.getRequestRecords().then(function(response){
    $scope.myVar.push(response);
    // do lots of things
});

As per my comment, an example use of $q.all:

// on check, wait for your three calls to be complete
// before triggering the callback
$scope.singleSelectSet=function(obj){
    var queue = [
        myService.getRequestRecords(),
        myService.getOtherRequest(),
        myService.getLastRequest()
    ];

    $q.all(queue).then(function(response){
        var responseRequestRecords = response[0],
            responseOtherRequest = response[1],
            responseLastRequest = response[2];
        // do lots of things
    });
}

Remember to inject $q in your controller.

  • There are 3 different rest calls after this response. I can't store it in an array. Is there any smoother way to do it? – RahulB Feb 13 '16 at 13:50
  • @RahulB You can group your 3 rest calls into one with [$q.all()](https://docs.angularjs.org/api/ng/service/$q#all) and update your $scope.myVar when that is done. – Thomas Ghesquiere Feb 13 '16 at 13:58