0

Steps:

  1. Get the roster(students) json data includes name,profile url from a web service
  2. Process the data and get profile image of each user via web service call by passing profile url
  3. Show the name and image of each user in window

While processing the data I need to pause the execution until I get an image from the 2nd web service call.I tried using a callback but couldn't achieve it.

below is the code snippet

//get roster data from web service:1
function getRosterData(callback) {

 var rosterToolURL; //web service url

 var xhrRosterTools = Ti.Network.createHTTPClient({
  onload : function() {
   var rosterJsonData = JSON.parse(this.responseText);
   //pass response to callback
   callback(rosterJsonData);
  },
  onerror : function(e) {
   Ti.API.info("STATUS: " + this.status);

  },
 });

 xhrRosterTools.open("GET", rosterToolURL);
 xhrRosterTools.send();
}

//process the roster data and call getUserImage for each row
function rosterWIndow(rosterJsonData) {

 var roster_collection = rosterJsonData.roster_collection;
 for ( i = 0; i < roster_collection.length; i++) {
  var rosterInfo = roster_collection[i];
  var imageURL = rosterInfo.imageUrl;
  //call get user
  getUserImage(imageURL, function(imageData) {
   Ti.API.info('after call back2' + imageData);

  });

 }

}

//get image from web service:2
function getUserImage(imageURL) {
 var xhrinfo = Ti.Network.createHTTPClient({
  onload : function() {
   Ti.API.info('response data' + this.responseData);
   return this.responseData;
  },
  onerror : function(e) {
   Ti.API.info("STATUS: " + this.status);

  },
 });

 xhrinfo.open("GET", imageURL);
 xhrinfo.send();
}
getRosterData(rosterWIndow);
Namphibian
  • 12,046
  • 7
  • 46
  • 76
  • It seems there is no "callback" input argument to your `getUserImage` function. – Olivier Oct 10 '14 at 15:10
  • Hi, Thanks for your reply. I have re-placed with callback(this.responseData); statement instead of return statement in getUserImage function. Still I am getting same problem. – srinu puramshetty Oct 11 '14 at 11:10
  • What I meant is that you should to replace the function definition `function getUserImage(imageURL)` by `function getUserImage(imageURL, callback)` if you want to be able to pass the callback as an argument. – Olivier Oct 11 '14 at 15:32
  • Hellow Olivier, " I just complicated the scenario. Simply I need to pass the loop until i get response. I achived it by following http://stackoverflow.com/questions/14408718/wait-for-callback-before-continue-for-loop" – srinu puramshetty Oct 13 '14 at 04:42

0 Answers0