0

更新

这是一个愚蠢的错字。我是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

我对 Backbone 有点陌生,所以谢谢你的帮助。:)

4

2 回答 2

1

问题是您的集合继承自错误的基类。

@ContentCollection: Backbone.Model.extend

应该

@ContentCollection: Backbone.Collection.extend
于 2012-12-04T07:07:19.817 回答
1

我不是咖啡脚本专家,但我认为你的问题是

@ContentCollection: Backbone.Model.extend

它应该是

@ContentCollection: Backbone.Collection.extend

此外,在迭代您的集合模型时,请使用

_.each(collection.models, function(model) { console.log(model); });
于 2012-12-04T07:09:14.690 回答