Skip to content

Backbone-relational compactibility with typescript #587

@aneer-anwar

Description

@aneer-anwar

I am trying to use Backbone-relational with typescript. My sample code looks like this :

`import * as Backbone from 'backbone';
import 'backbone-relational';

class Animal extends Backbone.RelationalModel{
constructor(options){
super(options);
this.urlRoot = '/animal/';
}

}

Animal.setup();

class AnimalCollection extends Backbone.Collection{
constructor(any){
super(any);
this.model = Animal;
}
}

class Zoo extends Backbone.RelationalModel{
constructor(options){
super(options);
this.relations =[{
type: Backbone.HasMany,
key: 'animals',
relatedModel: 'Animal',
collectionType: 'AnimalCollection',
reverseRelation: {
key: 'livesIn',
includeInJSON: 'id'
// 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'.
}
}];
}

}

Zoo.prototype.relations = [{
type: Backbone.HasMany,
key: 'animals',
relatedModel: 'Animal',
collectionType: 'AnimalCollection',
reverseRelation: {
key: 'livesIn',
includeInJSON: 'id'
// 'relatedModel' is automatically set to 'Zoo'; the 'relationType' to 'HasOne'.
}
}];

Zoo.setup();

//let artis = Zoo.build({ name: 'Artis' });

let artis = new Zoo({ name: 'Artis' });

//let lion = Animal.build( { species: 'Lion', livesIn: artis } );

let lion = new Animal( { species: 'Lion', livesIn: artis } );

alert( artis.get( 'animals' ).pluck( 'species' ) );
// artis.get( 'animals' ) undefined`

Unfortunately above code doesn't work. the _relations of artis gives empty object and throws error

Relation=child: missing model, key or relatedModel (class Zoo extends Backbone.RelationalModel {

please help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions