Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backbone-relational compactibility with typescript #587

Open
aneer-anwar opened this issue Aug 8, 2019 · 0 comments
Open

Backbone-relational compactibility with typescript #587

aneer-anwar opened this issue Aug 8, 2019 · 0 comments

Comments

@aneer-anwar
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant