使用Typescript在Backbone中定义集合的模型类型
我正在尝试将Backbone与Typescript一起使用,并且我没有遵循Typescript的语法来定义集合将持有哪种类型的模型.
I'm trying to use Backbone with Typescript, and I'm not following Typescript's syntax for how to define which type of model a collection will hold.
module Application.Collections {
export class Library extends Backbone.Collection {
model = Application.Models.Book; // THIS LINE DOESN'T WORK
constructor(options?) {
super(options);
};
}
}
module Application.Models {
export class Bookextends Backbone.Model {
constructor(options?) {
super(options);
}
}
}
我得到的错误是:
Type of overridden member 'model' is not subtype of original
member defined by type 'Collection';
我还不清楚我何时应该在构造函数中放置这样的定义,或者它是否重要.我试过了:
It's also not clear to me when I should put definitions like this in the constructor, or whether it matters. I tried:
constructor(options?) {
super(options);
this.model = Application.Models.Entity;
};
哪个说:
Cannot convert 'new(options?:any) => Models.Template' to 'Backbone.Model'
我真正想做的是定义一个具有一些实用方法的模型类型-从服务器获取基本模型数据时,我希望模型初始化并基于服务器计算一些便利属性数据.但这没有发生,因为collection.fetch()认为返回模型没有任何特定类型.
What I'm really trying to do is define a model type that has a few utility methods - when the basic model data is fetched from the server, I want the model to initialize and calculate a few convenience properties based on the server data. But this isn't happening, because the collection.fetch() doesn't think the return model is of any specific type.
在"super(options);"之前声明模型
Declare the model before "super(options);"
module Application.Models {
export class Book extends Backbone.Model {
constructor(options?) {
super(options);
}
}
module Application.Collections {
export class Library extends Backbone.Collection {
constructor(options?) {
this.model = Application.Models.Book;
super(options);
};
}
}