2013-04-09 111 views
0

我獲取集合的前15個記錄,並在單擊按鈕後爲同一集合獲取另外15個記錄。第二次後,集合的長度是15而不是30.即使正在加載15個新記錄,並且'add'事件完美工作。骨幹集合獲取不更新@ collection.length

代碼的第一時間:

@collection.fetch 
    data: @filter 
    update: true 
    success: => 
    @onSuccessCollection() 

代碼的第二時間:

loadMore: -> 
    @filter.skip = @collection.length 
    @collection.fetch 
    update: true 
    data: @filter, 
    success: => 
     @onSuccessCollection() 

成功回調(返回15第一次和15第二時間而30預計):

onSuccessCollection: -> 
    console.log 'onCollectionReset: collection.length: ', @collection.length 

我使用Backbone 0.9.10

回答

2

您必須將標誌remove設置爲false。 「智能升級」有刪除標誌默認爲真,所以當你只取15種型號,您的第一個15被刪除(因此長度等於15):

// Smartly update a collection with a change set of models, adding, 
// removing, and merging as necessary. 
update: function(models, options) { 
    options = _.extend({add: true, merge: true, remove: true}, options); 

Source

+0

認真地容易...非常感謝!完美的作品。 – 2013-04-09 13:06:04

+0

@MichaelKoper最難的部分是從舊版本獲取文檔:) – Loamhoof 2013-04-09 13:06:46

+0

是的確的...瀏覽源代碼是唯一的方法:) – 2013-04-09 13:17:26