使用TypeScript扩展微风实体 [英] Extending a breeze entity using TypeScript

查看:85
本文介绍了使用TypeScript扩展微风实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HotTowel和TypeScript开发一个网站。在约翰·帕帕(John Papa)出色的PluralSight课程中,他通过创建构造函数并使用 Object.defineProperty进行扩展来扩展了breezejs实体。例如,他添加了一个名为 fullName 的属性,如下所示。

I am developing a website using HotTowel and TypeScript. In John Papa's excellent PluralSight course, he extended a breezejs entity by creating a constructor and using 'Object.defineProperty' to extend it. For example, he added a property called fullName as follows.

NB:metadataStore是breezejs元数据存储区

NB: metadataStore is the breezejs metadata store

function registerPerson(metadataStore) {
        metadataStore.registerEntityTypeCtor('Person', Person);

        function Person() {
            this.isPartial = false;
            this.isSpeaker = false;
        }

        Object.defineProperty(Person.prototype, 'fullName', {
            get: function () {
                var fn = this.firstName;
                var ln = this.lastName;
                return ln ? fn + ' ' + ln : fn;
            }
        });
    }

我的问题是如何使用TypeScript实现相同的功能?

My question is how would I achieve the same using TypeScript ?

推荐答案

Typescript支持访问器开箱即用。您要求的TypeScript代码可能是:

Typescript support accessor out of the box. The TypeScript code you asked could be :

function registerPerson(metadataStore) {
    metadataStore.registerEntityTypeCtor('Person', Person);
}
class Person {
    public firstName: string;
    public lastName: string;

    constructor(public isPartial=false, public isSpeaker=false) {
    }

    get fullName():string {
        return `${this.firstName} ${this.lastName}`;
    }
}

这篇关于使用TypeScript扩展微风实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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