I'm having an error trying to add multiple models from the server to a collection. It throws the error:
Uncaught TypeError: Cannot read property 'idAttribute' of undefined
Here is a test example which gives same error:
<script type="text/javascript" src="js/jquery.3.2.1.min.js"></script>
<script type="text/javascript" src="js/underscore.1.8.3.min.js"></script>
<script type="text/javascript" src="js/backbone.1.3.3.min.js"></script>
<script type="text/javascript">
var Mymodel = Backbone.Model.extend({
defaults:{
id: null,
},
idAttributes: "id",
render: function(){
collection.add([{id:1, name: "name"},{id:2, name: "name"}])
}
});
mymodel = new Mymodel();
var mycollection = Backbone.Collection.extend({ model: mymodel });
collection = new mycollection();
mymodel.render();
</script>
The
modelproperty of the collection class should be a model constructor, not an instance.The model class has a
idAttributeproperty, singular, which is already'id'by default.Putting the
idattribute in thedefaultsisn't necessary and could lead to problems down the road.Other than the previous points, doing rendering within a model is bad practice. Use a view for this.
I understand this is a test example, but if it wasn't, a model class shouldn't change a global collection instance. The whole point of classes is to scope the responsibilities to the class itself and nothing outside of it.
Using a global like that just goes against the whole point of using Backbone.
Minimal code needed