0

我在咖啡脚本中有这段代码:

render: (collectionName)=>
  console.log "I am rendering"
  @buildBreadcrumb(collectionName) 

buildBreadcrumb: (collectionName) ->
  i=0
  _.each @urlParts, (url_part) =>
      i++
      console.log('value of i : ',i)
      collection = @getCollection(collectionName)
      if (collection.length == 0)
        collection.fetch({
          success: (collection) =>
            @render()
        })
      else
        @appendBreadcrumb()

而且我不明白为什么有时我的输出为:

I am rendering 
value of i : 1
value of i : 2
value of i : 3
/* STRANGE START HERE */
value of i : 2
value of i : 3

如果我在获取成功时删除 @render(),这个问题就会消失。就像 _each 循环又开始了……但是为什么呢?

如果我将@render 放在“完成”回调上,一切正常。

render: (route)=>
  console.log "I am rendering"
  @buildBreadcrumb() 

buildBreadcrumb: ->
  i=0
  _.each @urlParts, (url_part) =>
      i++
      console.log('value of i : ',i)
      collection = Proscale.Collections.getCollection(collectionName)
      collection.fetch({
        complete: (collection) =>
          @render()
      })
4

1 回答 1

1

试试这个,

render: ()=>
  console.log "I am rendering"
  @buildBreadcrumb(collectionName) 

buildBreadcrumb: (collectionName) ->
  i=0
  _.each @urlParts, (url_part) =>
      i++
      console.log('value of i : ',i)
      collection = @getCollection(collectionName)
      collection.fetch({
        success: (collection) =>
          @doSomething(collection)
      })

doSomething : (collection) ->
于 2012-09-24T12:50:08.727 回答