Search code examples
javascriptkendo-uikendo-mobile

Kendo: Extend ObservableObject with new properties


I have a kendo DataSource object from which I get an object. I would like to extend this object before binding it to a Kendo widget to provide additional fields.

var studentId = 4711
var student = studentDataSource.get(studentId);

kendo.bind($("#student-view"), student, kendo.mobile.ui);

E.g. student then looks like this:

{
    FirstName: "Peter",
    LastName: "Pan"
}

Somehow I would like to extend it to have it like this:

{
    FistName: "Peter",
    LastName: "Pan",
    DisplayName: function() {
        return this.get("LastName") + ", " + this.get("FirstName");
    }
}

Is this somehow possible or do I have to write every field of the object I retrieve from the datasource one by one into a new viewModel which I later on bind to the view?

Thanks in advance for your help!


Solution

  • It is possible by using the kendo datasource schema definition:

    var dataSource = new kendo.data.DataSource({
        schema: {
            model: {
                DisplayName: function() {
                    return this.get("LastName") + ", " + this.get("FirstName");
                }
            }
        }
    });
    

    Thanks to Lars Höppner for the hint!