Sunday, 18 August 2013

Getting around asynchronous ajax get

Getting around asynchronous ajax get

So I have this function -
IPGeocoding = (data) ->
coords = []
finish = _.after(data.length, (coords) ->
console.log coords
return coords
)
_.each(data, (datum) ->
$.ajax(
url: "http://freegeoip.net/json/#{datum}"
type: 'GET'
success: (result) ->
lat = result.latitude
lon = result.longitude
pair = [lat, lon]
coords.push(pair)
finish(coords)
)
)
It's being called like this
if @model.get('data')?
if @model.get('func')?
@points = @model.get('func')(@model.get('data'))
however, @points is undefined. I want @points to be what coords is when
console.log coords is run(which is an array of some length). I'm using
_.after because I want to build coords up from the results of multipl
async calls.
How do I get the coordinates to be in coords?

No comments:

Post a Comment