Javascript fetch未更新collection.model.models
我有一个主干集合,我已经初始化如下:Javascript fetch未更新collection.model.models,javascript,backbone.js,Javascript,Backbone.js,我有一个主干集合,我已经初始化如下: 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(myColl
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 []
myCollection = new MyCollection([], {type: 'animals', params: {username: 'steve'}});
myCollection.fetch({
success : function(returnedCollection, response){
console.log(returnedCollection);
console.log(returnedCollection.models);
}
});
有人知道为什么吗?获取是一个异步操作,因此,在获取之后立即执行的任何操作都很可能在获取完成之前执行,这会导致非常随机的结果。将控制台日志记录放在fetch的success函数中,看看会发生什么您的集合模型必须有一个指向服务器的url才能将其提取到集合中,我认为您在“MyCollection”上有它,以防万一。然后,您只需添加一个成功回调来显示已填充的集合,如下所示:
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 []
myCollection = new MyCollection([], {type: 'animals', params: {username: 'steve'}});
myCollection.fetch({
success : function(returnedCollection, response){
console.log(returnedCollection);
console.log(returnedCollection.models);
}
});
你的收集功能是什么样的?另外,为什么要传入一个空数组?可能是异步问题。类似于fetch的操作是一种异步操作,因此在fetch之后立即执行的任何操作都很可能在fetch完成之前执行,这会导致非常随机的结果。将控制台日志记录放在
fetch
的success
-函数中,然后查看发生了什么感谢@jake将其放在success工作中-是一个异步问题