Javascript Can';t遍历Backbone.js中的集合
更新 那是个愚蠢的打字错误。我使用的是Javascript Can';t遍历Backbone.js中的集合,javascript,backbone.js,coffeescript,Javascript,Backbone.js,Coffeescript,更新 那是个愚蠢的打字错误。我使用的是Backbone.Model.extend集合。掌纹 尝试迭代一个集合,但我认为我填充了不正确的集合或其他内容: RecentContent = Backbone.View.extend initialize: -> @collection = new ContentAPI.ContentCollection() @collection.fetch success: (collecti
Backbone.Model.extend
集合。掌纹
尝试迭代一个集合,但我认为我填充了不正确的集合或其他内容:
RecentContent = Backbone.View.extend
initialize: ->
@collection = new ContentAPI.ContentCollection()
@collection.fetch
success: (collection, response, options) =>
console.log @collection
# d {attributes: Object, _escapedAttributes: Object, cid: "c4", changed: Object, _silent: Object…}
# property `attributes` contains Objects from server
console.log @collection.models # undefined
@render()
#---------------------
render: ->
# ERROR: Object has no method 'each'
@collection.each (model) ->
console.log model
我还注意到,如果我尝试将reset
事件绑定到@collection
(而不是从success
回调中进行渲染),它似乎永远不会被触发
收藏非常简单:
class ContentAPI
@Content: Backbone.Model.extend {}
@ContentCollection: Backbone.Model.extend
url: "/api/content/"
model: @Content
我是个新手,所以谢谢你的帮助。:) 问题是集合继承自错误的基类
@ContentCollection: Backbone.Model.extend
应该是
@ContentCollection: Backbone.Collection.extend
我不是咖啡脚本专家,但我认为你的问题是
@ContentCollection: Backbone.Model.extend
应该是
@ContentCollection: Backbone.Collection.extend
此外,在迭代集合的模型时,使用
_.each(collection.models, function(model) { console.log(model); });
下划线方法被合并到集合原型中,因此他拥有
每个的方式很好。我也这么认为。但是我在主干网站上找不到文档了。你应该选择一个答案,即使你已经找到了答案。