使用Typescript在Backbone中定义集合的模型类型 [英] Define a collection's model type in Backbone using Typescript

查看:105
本文介绍了使用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);
            };
        }
    }

这篇关于使用Typescript在Backbone中定义集合的模型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆