0

Why does console.log data.markets give me an empty array [] for markets while after initialization of the JSON request I do get the complete array?

@refresh = (data) ->
  $table = @select('table')
  $table.prepend(JST['market'](market)) for market in data.markets
  console.log data.markets

The console.log markets is showing the data. What I am missing that the markets array is not passed over to @refresh?

@after 'initialize', ->

  $.getJSON "/api/v2/tickers", (data) ->

    markets = []
    for own cur of data
      ticker = data[cur].ticker
      item = {}
      item.volume = ticker.vol
      markets.push item
    console.log markets

  @.refresh {markets: markets}

Complete code here: http://pastebin.com/wXXC7utT

domi771
  • 450
  • 1
  • 9
  • 21
  • Because you're calling `refresh` [before the `getJSON` request finishes](http://stackoverflow.com/q/14220321/1048572)? I wonder that it's an array at all and not `undefined`. – Bergi Sep 11 '14 at 22:53
  • @Bergi - you are correct! if you make an answer I can mark it as solution – domi771 Sep 12 '14 at 08:59

1 Answers1

0

What I am missing that the markets array is not passed over to @refresh?

That you're calling refresh before the getJSON request finishes?! I wonder that it's an array at all and not undefined that you see.

@after 'initialize', ->
  $.getJSON "/api/v2/tickers", (data) ->
    markets = for own cur of data
      {volume: data[cur].ticker.vol}
    console.log markets
    @.refresh {markets: markets}
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375