Extending a breeze entity using TypeScript

允我心安 提交于 2019-12-12 01:54:00

问题


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 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;
            }
        });
    }

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


回答1:


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}`;
    }
}


来源:https://stackoverflow.com/questions/29041959/extending-a-breeze-entity-using-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!