PaulUithol / Backbone-relational

Get and set relations (one-to-one, one-to-many, many-to-one) for Backbone models
http://backbonerelational.org
MIT License
2.34k stars 330 forks source link

Backbone-relational compactibility with typescript #587

Open aneer-anwar opened 5 years ago

aneer-anwar commented 5 years ago

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