0

I have an Ionic application, that I'm trying to return data from inside a closure. The console is showing everything properly, but I can't seem to get the data to return properly. I've tried a few different variations with no luck.

$scope.callbackMethod = function (query) {
 //var ritems= new Array();

  var ritems;

  Inventory.search(query, $scope.currentUser.token, $scope.currentUser.user_id, function(response) {
 //console.log(JSON.stringify(response));
 if(response.success == true)
 {
    $ionicLoading.hide();
    $scope.requestmodal.hide();            
    console.log(response.items);
    ritems= response.items;    
  }
    else
  {
    $ionicLoading.hide();
    //console.log(response.message);
    return $scope.errorMessage = response.message;
   } 
  });

console.log(ritems);
return ritems;

};

and also this:

 $scope.callbackMethod = function (query) {
 //var ritems= new Array();

  var ritems =  Inventory.search(query, $scope.currentUser.token, $scope.currentUser.user_id, function(response) {
     //console.log(JSON.stringify(response));
     if(response.success == true)
     {
        $ionicLoading.hide();
        $scope.requestmodal.hide();

        console.log(response.items);
        return response.items;

      }
        else
      {
         $ionicLoading.hide();
         //console.log(response.message);
         return $scope.errorMessage = response.message;
       } 
    });

console.log(ritems);
return ritems;

};

the json that is returned to the script is:

{"success":true,"items":[{"id":"1","0":"1","name":"Product 1","1":"Product 1","ref_id":"","2":""},{"id":"2","0":"2","name":"Product 2","1":"Product 2","ref_id":"","2":""}],"message":""}

Any thoughts? Much appreciated!!

7
  • Please share your Inventory code also Commented Jun 23, 2015 at 16:08
  • Are you getting undefined in ritems Commented Jun 23, 2015 at 16:10
  • @Mohit I added the json response. Thank you! Commented Jun 23, 2015 at 16:12
  • @Mohit in the second example I do get undefined Commented Jun 23, 2015 at 16:13
  • 1
    You are getting undefined for both the cases because it is an asynchrounous call and the console.log runs before the response actuallly comes Commented Jun 23, 2015 at 16:32

1 Answer 1

1

You can follow this approach to solve your problem

var cbm = function (query) {
  var defer = $q.defer();
  Inventory.search(query, $scope.currentUser.token,$scope.currentUser.user_id, function(response) {
  if(response.success == true)
  {
    $ionicLoading.hide();
    $scope.requestmodal.hide();            
    defer.resolve(response.items) ;   
  }
  else
  {
    $ionicLoading.hide();
    defer.reject(response.message);
  } 
});
 return defer.promise;
};

and while calling you can then call your callbackMethod like

$scope.callbackMethod = function (query){ 
   return cbm(query).then(function(data){ return data;}); 
}
Sign up to request clarification or add additional context in comments.

8 Comments

Testing it now. Am I on the right track?: $scope.cbm= function (query) { var ritems; var defer = $q.defer(); Inventory.search(query, $scope.currentUser.token,$scope.currentUser.user_id, function(response) { if(response.success == true) { $ionicLoading.hide(); $scope.requestmodal.hide(); defer.resolve(response.items) ; } else { $ionicLoading.hide(); defer.reject(response.message); } }); return defer.promise; }; $scope.callbackMethod = cbm.then(function(data){ $scope.data = data });
It should work.but,where is it this "callbackMethod" called?
$scope.callbackMethod should be the call for it
yeah..I'm asking where do you use it in html?
this is placed in the controller for the angular script, but on the front-end <ion-autocomplete ng-model="requestdata.addeditem" value="" items-method="callbackMethod(query)" item-view-value-key="name" item-value-key="id" placeholder="Select An Item"/>
|

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.