未定义模型原型骨干收集和木偶CompositeView中 [英] Undefined model prototype in Backbone Collection and Marionette CompositeView
问题描述
尝试从值列表填充的集合,我正在对采集的模式
的原型$ C $错误C>被定义。看着有关类似问题这个问题,我已经检查了该模型的实际创建的前的集合被实例化,尽我的能力。
Trying to populate a Collection from a list of values, I am getting an error about the Collection's model
's prototype
being undefined. Looking at this question about a similar problem, I have checked that the Model is actually created before the collection is instanced, to the best of my ability.
该错误被抛出的木偶CompositeView中的事件处理程序保存集合中的一个,从服务器获取数据,并试图重置
收集后用从中应填充到它的数据值的列表。
The error is being thrown in one of the event handlers of the Marionette CompositeView that holds the Collection, after fetching the data from the server and trying to reset
the collection with the list of values from the data which should be populated into it.
的注:使用0.9.10骨干的
示范
MyItemModel = Backbone.Model.extend({});
收集
MyCollection = Backbone.Collection.extend({
model: MyItemModel
});
的CompositeView中的相关code
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();
});
}
})
错误
在发生错误的添加
的骨干作用,做当 GET
模型对象,检查以看它是否是一个重复的。失败的code是在这里:
The error happens in the add
function in Backbone, when doing a get
for the model object, checking to see if it is a duplicate. The failing code is here:
// 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
因为原型
为模型没有定义失败。
Here, the this.model.prototype.idAttribute
fails because the prototype
for the model is not defined.
这究竟是为什么,以及如何可以把它固定?
Why is this happening, and how can it be fixed?
非常感谢!
推荐答案
究其原因,在Babkbone 0.9.10,如果调用 collection.reset(机型)
无选项,该机型将被传递到 collection.add()
这需要严格的实时模型作为参数。
The reason is, in Babkbone 0.9.10, if you call collection.reset(models)
without options, the models will be passed to collection.add()
which strictly needs real models as argument.
但事实上,你传递的参数不是真正的机型。它们只是散列属性的数组
But, in fact, the arguments you passed are not real models. They are just an array of hash attributes.
两个选项来解决:
that.collection.reset(collection_results, {parse: true});
然后重置
将解析哈希值的阵列,并将它们设置为模型。
Then reset
will parse the array of hashes and set them as model.
下面复位()
不再把责任到添加()
,但使用集()
巧妙。建议使用此选项。而且你不需要选择这里。
Here reset()
no longer pass responsibility to add()
but use set()
smartly. This option is recommended. And you don't need options here.
that.collection.reset(collection_results)
还有一点
我建议你不要在CompositeView中的定义模式
? CompositeView中是收藏,而不是模型。当然,我理解这里的模型仅仅是保持并获取一些数据,但它会为code真的迷惑其他开发人员读取,以及你自己的维护。
Another point
May I suggest you not to define model
in CompositeView? CompositeView is for collection, not model. Of course I understand the model here is just to hold and fetch some data, but it would be really confusing for the code to be read by another developer, as well as your own maintaining.
要获得自举的数据,你可以在第一次请求加载数据,并使用常规的方式把它变成集。 http://backbonejs.org/#FAQ-bootstrap
To get bootstrapped data, you can load the data at first request and use conventional way to put it into collection. http://backbonejs.org/#FAQ-bootstrap
这篇关于未定义模型原型骨干收集和木偶CompositeView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!