2012-07-09 34 views
0

我有一個骨幹集合,我已經像這樣初始化:取不更新collection.model.models

myCollection = new MyCollection([], {type: 'animals', params: {username: 'steve'}}); 

myCollection.fetch(); 

console.log(myCollection) // prints out an object that includes 'models' and the newly fetched models 

console.log(myCollection.models) // prints out an empty list [] 

沒有人知道爲什麼嗎?

+0

那麼你的收藏功能是什麼樣子?另外,你爲什麼傳入一個空數組? – Austin 2012-07-09 15:30:40

+0

可能是一個異步問題。類似於http://stackoverflow.com/questions/9584870/backbone-js-fetch-not-actually-setting-attributes/9585427#9585427 – nikoshr 2012-07-09 15:32:14

+3

fetch是一個異步操作,因此無論您在獲取後立即執行操作,最有可能在執行抓取完成,導致相當隨機的結果。將控制檯日誌記錄到'fetch'的'success'-函數中,看看會發生什麼 – jakee 2012-07-09 15:33:18

回答

2

fetch是一個異步操作,所以無論您在抓取後立即執行操作,最有可能在抓取完成之前執行,這會導致相當隨機的結果。將控制檯日誌記錄到獲取的成功函數中,看看會發生什麼

0

您的集合的模型必須具有服務器的網址才能將其獲取到集合中,我認爲您在「MyCollection」上擁有它,案件。然後你只需要添加一個成功的回調顯示填充集合,像這樣:

myCollection = new MyCollection([], {type: 'animals', params: {username: 'steve'}}); 

myCollection.fetch({ 
    success : function(returnedCollection, response){ 
     console.log(returnedCollection); 

     console.log(returnedCollection.models); 
    } 
});