2012-04-05 66 views
0

我有,我認爲是簡單的診斷問題的功能範圍內,但我似乎無法得到它在我自己的。骨幹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函數。

有什麼想法?如果我缺少關鍵信息,請告訴我。提前致謝。

邁克爾

回答

0

你想要的結構是隻綁定視圖的render爲你有reset,也許add/remove以及根據應用程序的具體收集的事件。然而,getActivities應該簡單地告訴收集通過fetch方法來獲取數據,但使用當前的一組過濾器(我假設是搜索查詢)。因此,添加一個名爲filters到您的收藏類實例屬性,在getActivitiesevents.filters = FD.filters,然後就events.fetch()。當響應到達集合將重置,這將觸發一個事件,這將導致render()執行,瞧,你的看法會是最新的。您在render()中進行ajax請求的代碼屬於集合,而不是視圖。