coffeescript缩进与循环

时间:2014-09-11 15:29:48

标签: javascript coffeescript

我有以下js.coffee / Coffeescript,我想把市场数据作为输出:

window.MarketsUI = flight.component ->
  @defaultAttrs
    table: 'tbody'
    filter: '.dropdown-menu a'

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

  @filter = (event) ->
    type = event.target.className
    return @list.filter() if type == ''

    @list.filter (item) ->
      item.values().type == "#{gon.i18n[type]}"

  @initList = ->
    options =
      valueNames: [ 'market', 'currency', 'vol', 'change',
      'last', 'high', 'low']
    @list = new List('marketsX', options)

  @after 'initialize', ->

    markets = []
    $.getJSON "/api/v2/tickers", (data) ->
     #markets = []
     for cur of data
       ticker = data[cur].ticker
       item = {}
       [
         "change"
         "last"
         "high"
         "low"
       ].forEach (key) ->
         item[key] = ticker[key]
         return

       item.volume = ticker.vol
       item.market = cur
       item.currency = cur.substring(3)
       markets.push item
       console.log markets

   markets.sort (a, b)->
     a.volume - b.volume

   @refresh {markets: markets}

   @initList()

   @on @select('filter'), 'click', @filter

我的问题是当我在@refresh函数中控制登录时,市场对象是空的。我只得到[]。我知道它与coffeescript的缩进有关,因为它在纯JS中工作。当我对市场数据进行核心时,它也在发挥作用。

1 个答案:

答案 0 :(得分:0)

是的,有一个显而易见的(至少当你看一下编译后的JS)缩进问题:

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

需要

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

此外,看起来markets.sort@refresh@initlist来电需要进入ajax回调,但我不确定你想要什么(而且你还没有#39;告诉我们)。