I’ve got a simple Backbone collection that pulls a list of objects from a (remote) resource. Calling Collection.fetch, though, fails with this error:
Object [object Object] has no method '_validate'
I’m guessing this is happening under the hood when the collection attempts to create a model instance for each JSON object while adding. Can anyone shed some light on why this would happen?
Here’s the code I’m using. Very bare bones…
/* Models */
var SomeModel = Backbone.View.extend({});
/* Collections */
var SomeCollection = Backbone.Collection.extend({
url: 'http://localhost:8000/api/some/resource/?format=json',
model: SomeModel,
parse: function(data) {
return data.objects
}
});
var SomeView = Backbone.View.extend({
collection: new SomeCollection(),
initialize: function() {
this.collection.fetch();
},
});
And here’s an example of the resource response:
{
"meta": {
"count": 100
},
"objects": {
{"title": "Title", "id": 1},
{"title": "Title 2", "id": 2}
}
}
You probably didn’t set your collection’s
modeloption to a valid Backbone.Model subclass. When the collection fetches the data it will instantiate models and callset, which will call_validate, which a validBackbone.Modelsubclass will have, but your instances do not. See the annotated source code for Backbone.Collection.fetch for details.Thanks for posting code. here’s the problem. Your
SomeModelextendsViewinstead ofModel. My guess is copy/paste/forget-to-edit.