我有,我認爲是簡單的診斷問題的功能範圍內,但我似乎無法得到它在我自己的。骨幹JS重置的視圖集合但從
我已經寫在CoffeeScript中以下簡稱骨幹代碼:
events = FD.Events # collection
# instantiated on page load with collection: events
class FD.StreamView extends Backbone.View
el: $(".stream")
initialize: ->
# Bind the view to listen for a reset of the collection and rerender
@collection.on("reset", @render)
# Listen for a change to this model and run the function getActivities
FD.filters.on("change", @getActivities)
render: =>
# render function code
getActivities: =>
# create a new instance of the FD.Events collection class
events = new FD.Events
# create a new instance of this view, passing in the new empty collection
streamView = new FD.StreamView collection: events, model: FD.filters
# make an AJAX call and as a part of the callback function, reset the collection
# for the view we just instantiated above
params = FD.filters.attributes
callbackFunc = (responseText) ->
json = $.xml2json(responseText)
activities = json.activity
events.reset activities
params = FD.filters.attributes
activities_json = $.get("/landing/Data", params, callbackFunc)
的問題,因爲你可能會看到,就是getActivities函數運行的時間量的兩倍每個集合被調用時,因爲第一次運行它會創建第二個視圖,然後運行這兩個視圖。當集合發生變化時,這兩個實例中的每一個都會創建兩個實例,所以我們現在有四個實例,並且全部四個都運行getActivities。然後是8,然後是16.
明確地創建FD.StreamView類的新實例並不是正確的方法,但我不知道如何重置單個視圖綁定的集合,以及如何然後可以觸發綁定的@render函數。
有什麼想法?如果我缺少關鍵信息,請告訴我。提前致謝。
邁克爾