This model is undefined when trying to add an object to the base collection

I was wondering if there is anything that I should keep in mind when using the core network? I basically have a model, and my collection is defined as:

LibraryPreps = (function () { return Backbone.Collection.extend({ model: LibraryPrep, url: '/api/platform', initialize: function (models, options) { } }); })(); LibraryPrep = (function () { return Backbone.Model.extend({ defaults: { name: '', platform: '', }, initialize: function () { return this; } }); })(); 

nothing fancy about them. When I create a LibraryPrep and register it, it looks like the data I want. But when I try to add it to the collection, I get this error:

 TypeError: this.model is undefined followed by this line of code: this._idAttr || (this._idAttr = this.model.prototype.idAttribute); 

I basically do this:

 var libPreps = new LibraryPreps(); _.each(libraries, function (library) { console.log("POPULATE LIBRARY PREP"); console.log(library); var tempLibPrep = new LibraryPrep(library); console.log(tempLibPrep); libPreps.add(tempLibPrep); // why aren't you working?! }); 

I used the collection somewhere even earlier, and I never had a problem with it. I am new to the Internet, although perhaps there is only something that I don’t think about. Any thoughts? Thanks in advance: -.

0
source share
2 answers

Take a look at LibraryPreps.prototype and you will see where you are mistaken. First of all, your real code should look bigger, or you will get ReferenceError s:

 var LibraryPreps = (function () { ... })(); var LibraryPrep = (function () { ... })(); 

When the anonymous function executing LibraryPreps is executed, LibraryPrep will be undefined because it has not been assigned a value until a later time. If you do this:

 var LibraryPreps = (function () { return Backbone.Collection.extend({ model: LibraryPrep, //... }); })(); var LibraryPrep = (function () { return Backbone.Model.extend({ /*...*/ }); })(); console.log(LibraryPreps.prototype); 

you will see that LibraryPreps.prototype.model is undefined in the console. Demo: http://jsfiddle.net/ambiguous/y8cja/

Calling Backbone.Collection.extend (with or without an anonymous self-learning shell of functions) causes LibraryPrep evaluate when extend called, so you create a "class" collection with an undefined model . Then, inside the Backbone, it will search for the idAttribute the collection model and you will get your error.

Correct the order of your definitions so that everything is defined before they are used:

 var LibraryPrep = (function () { ... })(); var LibraryPreps = (function () { ... })(); 

and you will get better results.


In the Loamhoof comments , your code works fine with the current version of Backbone (1.0.0), and I cannot find this:

 this._idAttr || (this._idAttr = this.model.prototype.idAttribute); 

anywhere in the source 1.0.0. Presumably you are using an older version of Backbone, the Collection#add method should know the idAttribute property of your model.

+2
source share

Have you tried adding models directly to the collection?

 libPreps.add(libraries); 

http://backbonejs.org/#Collection-add

addcollection.add (models, [options]) Add a model (or an array of models) to the collection by triggering the add event. If a model property is defined, you can also pass raw attribute objects and animate them as instances of the model . Navigate {at: index} to group the model at the specified index. If you add models to the collection that are already in the collection, they will be ignored if you do not pass {merge: true}, in which case their attributes will be combined into the corresponding models, dismissing any corresponding "changes", events.

0
source share

All Articles