"未捕获类型错误:未定义不是一个函数"初始化骨干集合 [英] "Uncaught TypeError: undefined is not a function" initializing Backbone collection
问题描述
我有一个骨干集合类似以下内容:
I have a Backbone collection something like the following:
var FooCollection = Backbone.Collection.extend({
model:Foo,
initialize: function (attributes, options) {
this.barId = options.barId;
}
});
var Foo = Backbone.Model.extend({});
当我尝试初始化,我得到未捕获类型错误:未定义不是一个函数中的 _ prepareModel()
<$ C $的功能C> Backbone.Collection 。
When I try to initialize this, I get "Uncaught TypeError: undefined is not a function" in the _prepareModel()
function of Backbone.Collection
.
坏的通话中模式=新this.model(ATTRS,期权)
。
// 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.model
,事实上,不确定的。
When I step through _prepareModel()
in the debugger, it looks like the type of this
at that point is child
, and this.model
is, in fact, undefined.
谁能告诉我什么,我做错了吗?
Can anyone tell me what I'm doing wrong?
推荐答案
在我的实际code 富
在 FooCollection 被宣布code>。并没有意识到,JavaScript不支持前置声明。 [headdesk]
In my actual code Foo
was declared after FooCollection
. Didn't realize that Javascript doesn't support forward declarations. [headdesk]
这篇关于&QUOT;未捕获类型错误:未定义不是一个函数&QUOT;初始化骨干集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!