2014-07-14 56 views
1

一切看起來都正確,但每當我做下面的事情時,我得到TypeError:f在主幹1.1.2中未定義,另外,當我調試時,我看到它能夠接收到正確的響應,但是當我做categories.toJSON()我得到什麼:Backbone collection.fetch()導致TypeError:this.model未定義

var categories = new App.Lookup.CategoryCollection(); 
categories.url = 'index.php?r=lookupapi/categories'; 
categories.fetch(); 

App.Lookup.CategoryCollection = Backbone.Collection.extend({ 
    model: App.Lookup.CategoryModel     
}); 

App.Lookup.CategoryModel = Backbone.Model.extend({ 

}); 

我在做什麼錯在這裏?

更新:

我用的是開發版本由user972的建議,並得到了以下錯誤消息:

類型錯誤:this.model未定義

categories.fetch()是一個引起錯誤,它指向這個功能在主幹:

// Define how to uniquely identify models in the collection. 
    modelId: function (attrs) { 
     return attrs[this.model.prototype.idAttribute || 'id']; 
    }, 

但我仍然不明白爲什麼它是未定義的。

回答

0

出於好奇,你有沒有按照正確的順序引用腳本文件?它似乎更多來自腳本加載錯誤比基於代碼。

<script src="underscore-1.6.0-min.js"></script> 
<script src="backbone-1.1.2-min.js"></script> 

可能有幫助的其他事情是使用非最小版本的骨幹網,並嘗試找到它崩潰的地方,爲什麼?

<script src="http://backbonejs.org/backbone.js"></script> 
+0

是的,它是: Corbee

+0

請嘗試使用非最小版本並將斷點放在崩潰位置以查看要傳遞給哪些值哪些功能? – Ash

+0

我更新了我的問題,並提供了更多詳細信息=) – Corbee

1

App.Lookup.CategoryModel聲明沒有在上面App.Lookup.CategoryCollection懸掛。在使用之前,您需要定義App.Lookup.CategoryModel

所以你的代碼更改爲:

App.Lookup.CategoryModel = Backbone.Model.extend(); 

App.Lookup.CategoryCollection = Backbone.Collection.extend({ 
    model: App.Lookup.CategoryModel     
}); 

var categories = new App.Lookup.CategoryCollection(); 
categories.url = 'index.php?r=lookupapi/categories'; 
categories.fetch(); 

迴應:

I saw that it was able to receive the correct response, yet when I do  
categories.toJSON() I get nothing 

如果你想看到的fetch的結果,你需要通過success回調:

categories.fetch({ 
    success: function(collection, response, options) { 
     console.log(collection.toJSON()); 
    } 
}); 
+0

我遇到了同樣的問題。就我而言,我完全省略了Backbone.Model定義;通過將其添加到正確的位置來進行修正。感謝您指點我正確的方向。 – sockmonk