Skip to content

reverseRelation Collection is always instanceof Backbone.Collection #536

@mauron85

Description

@mauron85

Description: reverseRelation Collection is always instanceof Backbone.Collection

Expected: reverseRelation Collection is instanceof Routes
Actual: reverseRelation Collection is instanceof Backbone.Collection only

How to reproduce:

jsFiddle example: http://jsfiddle.net/mauron85/8uhvyq9L/

var User = Backbone.RelationalModel.extend({
        defaults: {
            routes: []
        }
});

var Route = Backbone.RelationalModel.extend({
    relations:[{
            type: Backbone.HasOne,
            key: 'user',
            relatedModel: User,
            includeInJSON: 'id', //serialize only id not a full object
            reverseRelation: {
                key: 'routes',
                includeInJSON: 'id'
            }        
    }]
});

var Users = Backbone.Collection.extend({
    model: User,
  url: '/users'
});

var Routes = Backbone.Collection.extend({
    model: Route,
    url: '/routes',
    someMethod: function () {
        console.log('you just called some method');
    }
});

var userJSON = [{
    id: 'john'
},{
    id: 'pete'
}];

var routesJSON = [{
    id: 'first',
    user: 'pete'
},{
    id: 'second',
    user: 'john'
}];

var users = new Users(userJSON);
var routes = new Routes(routesJSON);

var johnRoutes = users.at(0).get('routes')
console.log(johnRoutes.at(0).get('id') === 'second'); // TRUE
console.log(johnRoutes instanceof Backbone.Collection); // TRUE,
console.log(johnRoutes instanceof Routes); // FALSE, we cannot call Routes methods!!!

johnRoutes.someMethod(); // Uncaught TypeError: johnRoutes.someMethod is not a function

Modified version with model collection as constructor option throws:
Uncaught TypeError: collection.on is not a function
jsFiddle: http://jsfiddle.net/mauron85/8uhvyq9L/4/

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions