Undefined prototype model in the Backbone and Marionette CompositeView collections

Attempting to populate a collection from a list of values, I get the error message β€œ model prototype undefined collection. Considering this issue of a similar problem , I checked that the model was actually created before the collection was installed as much as possible.

The error is raised in one of the Marionette CompositeView event handlers, which contains the collection, after collecting data from the server and tries to reset collection with a list of values ​​from the data that must be filled into it.

Note. Using Backbone 0.9.10

Model

 MyItemModel = Backbone.Model.extend({}); 

Collection

 MyCollection = Backbone.Collection.extend({ model: MyItemModel }); 

Corresponding 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(); }); } }) 

Mistake

An error in the add function in the Backbone when executing get on a model object, checking if it is a duplicate The error 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); 

Here this.model.prototype.idAttribute fails because prototype not defined for the model.

Why is this happening, and how can this be fixed?

Thank you so much!

+8
javascript prototype undefined marionette
source share
1 answer

The reason is that in Babkbone 0.9.10, if you call collection.reset(models) without parameters, the models will be passed to collection.add() , which strictly needs real models as an argument.

But in fact, the arguments you conveyed are not real models. This is just an array of hash attributes.

Two fix options:

Option 1: Call reset with the parsing option

 that.collection.reset(collection_results, {parse: true}); 

Then reset will parse the hash array and set them as a model.

Option 2: upgrade to the latest version of Backbone 1.1.0.

Here, reset() stops responding to add() , but use set() cleverly. This option is recommended. And you do not need options here.

 that.collection.reset(collection_results) 

Another point

Can I suggest you not define model in CompositeView? CompositeView is for the collection, not the model. Of course, I understand that the model here is just for storing and receiving some data, but it would be very difficult to use the code for reading by another developer, as well as for your own maintenance.

To get the downloaded data, you can download the data at the first request and use the usual way to put it in a collection. http://backbonejs.org/#FAQ-bootstrap

+5
source share

All Articles