Backbone.js 主干集合.fetch()导致TypeError:this.model未定义
一切似乎都是正确的,但每当我执行以下操作时,我都会得到TypeError:f在主干网1.1.2中未定义,而且,当我调试时,我看到它能够收到正确的响应,但当我执行categories.toJSON()时,我什么也得不到:Backbone.js 主干集合.fetch()导致TypeError:this.model未定义,backbone.js,Backbone.js,一切似乎都是正确的,但每当我执行以下操作时,我都会得到TypeError:f在主干网1.1.2中未定义,而且,当我调试时,我看到它能够收到正确的响应,但当我执行categories.toJSON()时,我什么也得不到: var categories = new App.Lookup.CategoryCollection(); categories.url = 'index.php?r=lookupapi/categories'; categories.fetch(); App.Lookup.C
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建议的开发版本,并收到以下错误消息:
TypeError:此.model未定义
categories.fetch()是导致错误的原因,它指向主干中的此函数:
// Define how to uniquely identify models in the collection.
modelId: function (attrs) {
return attrs[this.model.prototype.idAttribute || 'id'];
},
但是我仍然不明白为什么它是未定义的。只是出于好奇,您是否按照正确的顺序引用了脚本文件?这似乎更多的是由于脚本加载错误,而不是基于代码
<script src="underscore-1.6.0-min.js"></script>
<script src="backbone-1.1.2-min.js"></script>
另一个可能有用的方法是使用非min版本的主干网,并尝试定位它崩溃的位置以及原因
<script src="http://backbonejs.org/backbone.js"></script>
未将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());
}
});
是的,它是:请尝试使用非最小版本,并将断点放在崩溃的位置,以查看哪些值被传递到哪个函数?我也遇到了同样的问题。在我的例子中,我完全省略了主干;通过将其添加到正确的位置进行修复。谢谢你给我指明了正确的方向。