2013-08-27 53 views
1

我正在使用coffeescript。我的代碼非常簡單:Backbone.js集合提取'this._byId'undefined

class SomeCollection extends Backbone.Collection 
    constructor: (@options) -> 
    url: -> 
    "#{$SCRIPT_ROOT}/some/data/#{@options.someId}" 
    model: SomeModel 

class SomeView extends Backbone.View 
    initialize: -> 
    myCollection = new SomeCollection() 
    myCollection.fetch 
     success: (coll, resp) -> 
     console.log coll 

那就是BEING從我收集的網址傳回的JSON正是:

[{"id": 1, "comments": "", "name": "images/exceptions/59.png"}]

然而,什麼是打印到控制檯前,我收到了Backbone.js的錯誤line 768無法讀取屬性1的undefined。該集合的get函數中的未定義對象是this._byId。我怎麼解決這個問題?

回答

5

您正在擴展Backbone.Collection並提供您自己的構造函數,因此您需要確保調用父構造函數。

constructor: (@options) -> 
    super null, @options 

此外,集合的標準參數是(models, options),所以我會堅持。

constructor: (models, @options) -> 
    super models, @options 

或者更好的是,使用的initialize代替constructor避免完全

initialize: (models, @options) -> 
+0

賓果!感謝您的簡潔而完整的解釋。 – Pakman