This is a migrated thread and some comments may be shown as answers.

defining a Kendo Model in TypeScript

1 Answer 449 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dean
Top achievements
Rank 1
Dean asked on 19 May 2017, 05:01 AM

Hi,

I have a TypeScript class that I want to use in a DataSource for the schema.model property but also for initialising objects that are strongly typed in the rest of my TypeScript code. An example of such a class that seems to work is as follows:

 

export class Person extends kendo.data.Model.define({
        id: "id",
        fields: {
            id: { type: "string" },
            firstName: { type: "string" },
            lastName: { type: "string" },
            age: { type: "number" }
        }
    }) {
    id: string;
    firstName: string;
    lastName: string;
    age: number;
     
    constructor(id?: string, firstName?: string, lastName?: string, age?: number) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

 

However, having the properties defined twice, once for the call to kendo.data.Model.define and then again for the sake of the TypeScript class is not ideal. Is there a better way to do this?

The following would be better if it created the field definitions automatically, but unfortunately it doesn't:

export class Person extends kendo.data.Model {
    id: string;
    firstName: string;
    lastName: string;
    age: number;
     
    constructor(id?: string, firstName?: string, lastName?: string, age?: number) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

 

Alternatively, is it possible to move the kendo.data.Model.define() call to inside the constructor?

Thanks

Dean

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 22 May 2017, 04:23 PM

Hello Dean,

I am afraid that you are correct and they should be define twice in order to work as expected. 

Regards,
Boyan Dimitrov
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
General Discussions
Asked by
Dean
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or