我在coffeescript中有这段代码:
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()
})
答案 0 :(得分: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) ->