Matteo Piazza
Matteo Piazza

Reputation: 2502

KendoUI: programmatically setting a datasource model

I would like to set programmatically the model of my datasource. Something like:

var ds = new kendo.data.DataSource({ //... });

var mod = kendo.data.Model.define({
    fields: data 
});

ds.model = mod;

Is it possible? How? Thank you.

Upvotes: 3

Views: 8609

Answers (1)

Samuel Caillerie
Samuel Caillerie

Reputation: 8275

Of course, but you have to set it up in DataSource field schema.model (see schema.model reference)

As indicated on this page, you will have something like this :

// Definition of your model
var Product = kendo.data.Model.define({
    id: "ProductID",
    fields: {
         ProductID: {
            //this field will not be editable (default value is true)
            editable: false,
            // a defaultValue will not be assigned (default value is false)
            nullable: true
         },
         ProductName: {
             validation: { //set validation rules
                 required: true
             }
         },
         UnitPrice: {
           //data type of the field {Number|String|Boolean|Date} default is String
           type: "number",
           // used when new model is created
           defaultValue: 42,
           validation: {
               required: true,
               min: 1
           }
       }
   }
});

// Map this model to your DataSource object
var dataSource = new kendo.data.DataSource({
    schema: {
        model: Product // Use the existing Product model
    }
});

Upvotes: 4

Related Questions