Backbone-relational icon indicating copy to clipboard operation
Backbone-relational copied to clipboard

Backbone-relational compactibility with typescript

Open aneer-anwar opened this issue 5 years ago • 0 comments

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

aneer-anwar avatar Aug 08 '19 11:08 aneer-anwar