【发布时间】:2013-12-20 18:04:16
【问题描述】:
尝试从值列表填充集合时,我收到关于集合的model 的prototype 未定义的错误。查看this question about a similar problem,我尽我所能检查了模型实际上是在集合被实例化之前创建的。
在从服务器获取数据并尝试reset 包含应填充数据的值列表的集合之后,在包含集合的 Marionette CompositeView 的事件处理程序之一中引发错误进入它。
注意:使用 Backbone 0.9.10
模型
MyItemModel = Backbone.Model.extend({});
收藏
MyCollection = Backbone.Collection.extend({
model: MyItemModel
});
CompositeView的相关代码
MyCompositeView = Backbone.Marionette.CompositeView.extend({
initialize: function(options) {
_.bindAll(this);
this.model = new MyCompositeViewModel();
this.collection = new MyCollection();
},
//This event handler gets properly fired and run.
on_event: function() {
var that = this;
// The data comes through fine with this `fetch`
this.model.fetch({success: function() {
var collection_results= that.model.get("collection_results");
// The error fires in this line
that.collection.reset(collection_results);
that.render();
});
}
})
错误
错误发生在 Backbone 中的 add 函数中,当为模型对象执行 get 时,检查它是否是重复的。失败的代码在这里:
// Get a model from the set by id.
get: function(obj) {
if (obj == null) return void 0;
// The error originates from this line
this._idAttr || (this._idAttr = this.model.prototype.idAttribute);
return this._byId[obj.id || obj.cid || obj[this._idAttr] || obj];
},
this._idAttr || (this._idAttr = this.model.prototype.idAttribute);
这里,this.model.prototype.idAttribute 失败,因为模型的 prototype 未定义。
为什么会发生这种情况,如何解决?
非常感谢!
【问题讨论】:
-
您能否在引发错误的行周围包含完整的代码以获得一些上下文?
-
出错的行是什么文件名和行号?
-
添加了代码上下文。 @einnocent,backbone.js 中的行是 710。
-
没有您在 Backbone 0.9.2 中提到的代码。 github.com/jashkenas/backbone/blob/0.9.2/backbone.js你确定吗? (只需搜索带有“_idAttr”的页面)
-
@BillyChan:没错!实际上使用0.9.10。感谢您的提醒!
标签: javascript backbone.js prototype undefined marionette