I have a datagrid that holds businesses information, and I have a button that a user will click that causes an API call to be run for every row.
First off, here is my code.
newHttp.addEventListener(ResultEvent.RESULT, addWebsiteToGrid);
var location:String = zip.text;
for (var i:int = 0;i<dpData.length;i++) {
var s:String = replaceString(dpData*[0], " ", "%20");
var url:String = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+location+"%20"+s;
newHttp.url = url;
newHttp.send();
}
private function addWebsiteToGrid(e:Event):void {
var JSONResArr:String = String(http.lastResult);
var obj:Object = JSON.decode(JSONResArr);
dpData[entryCount][4] = obj.responseData.results[0].url;
dg.dataProvider = dpData;
}
Now, the trouble I’m having is this: The search runs well, but I’m only getting one result back. It gets populated in the first row, but it’s from an item that’s about 10 rows down. Every time this is run, it returns the same result… so I’m guessing what’s happening is the event is getting the response for the 10th row call, but it’s getting inserted into the wrong row at the wrong time.
I don’t know exactly how I can rectify this. Maybe there’s a way I can wait to do the next row’s query until the response for the current one has been received?
How might I do something like that?