我有一个 Backbone 集合,如下所示:
var FooCollection = Backbone.Collection.extend({
model:Foo,
initialize: function (attributes, options) {
this.barId = options.barId;
}
});
var Foo = Backbone.Model.extend({});
当我尝试初始化它时,我在
_prepareModel()
中得到“Uncaught TypeError: undefined is not a function” Backbone.Collection
的功能.坏电话在
model = new this.model(attrs, options)
.// Prepare a model or hash of attributes to be added to this collection.
_prepareModel: function(model, options) {
options || (options = {});
if (!(model instanceof Model)) {
var attrs = model;
options.collection = this;
model = new this.model(attrs, options); // <-- BLOWS UP HERE
if (!model._validate(model.attributes, options)) model = false;
} else if (!model.collection) {
model.collection = this;
}
return model;
},
当我通过
_prepareModel()
在调试器中,它看起来像 this
的类型那时是 child
, 和 this.model
实际上是未定义的。谁能告诉我我做错了什么?
最佳答案
在我的实际代码中 Foo
在 FooCollection
之后声明.没有意识到 Javascript 不支持前向声明。 【前台】
关于backbone.js - "Uncaught TypeError: undefined is not a function"初始化 Backbone 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162605/