I have two models (User and Task) which are instances of Backbone.RelationalModel
.
The relation about these two models is the following:
// Task model
var Task = Backbone.RelationalModel.extend({
relations: [
{
type: 'HasOne',
key: 'user',
relatedModel: User
}
],
urlRoot: 'someUrl'
});
Then I have one collection which code looks like this:
var FollowerCollection = Backbone.Collection.extend({
initialize: function () {
_.bindAll(this);
}
model: User
});
var User = Backbone.RelationalModel.extend({
});
When I make a fetch on FollowerCollection I get the following error:
Uncaught TypeError: Cannot read property 'idAttribute' of undefined
on the line 1565 of backbone-relation.js of backbone-relation version 0.5.0
Here a piece of code of backbone-relation.js
if ( !( model instanceof Backbone.Model ) ) {
// Try to find 'model' in Backbone.store. If it already exists, set the new properties on it.
var existingModel = Backbone.Relational.store.find( this.model, model[ this.model.prototype.idAttribute ] );
The problem is related to _.bindAll(this)
because if I comment it, it works properly.
Why? Any ideas?
User
model defined when you declare the relation? – Hendrickson_.bindAll(this)
. can you explain me why it does brake the code? I did change my question title. – Norway_.bindAll(this)
bindsthis
to every function in your Collection, so anything could happen. Most likely it leads tothis.model
orthis.model.prototype
being something other than Backbone-relational expects them to. Is the bindAll necessary? If it is necessary for some function in your collection do_.bindAll(this, 'funcname1', 'funcname2', ..., 'funcnameN')
to single out the functions you need to bindthis
to. – Federal