Telerik UI for Windows 8 HTML

To enable Telerik.Data.DataSource to perform data editing operations, you need to configure its schema.model and transport.

Configure the DataSource for remote CRUD (Create, Read, Update, Destroy) data operations

The DataSource is configured for remote CRUD operations, using the transport.create, transport.update and transport.delete options. Below you can see the available options and their meaning:

transport.create is the configuration used when the DataSource saves newly created data items. Those are items added to the data source via the add or insert methods (these could be called implicitly by a Telerik control like RadGrid during editing).

transport.update is the configuration used when the data source saves updated data items. Those are data items whose fields have been updated.

transport.destroy configuration used when the data source destroys data items. Those are items removed from the data source via the remove method (it could be called implicitly by a Telerik control like RadGrid during editing).

These properties accept a string, an Object or a function as a parameter:

  • If the value is a function the data source invokes that function instead of making an AJAX call.

  • If the value is a string the data source uses this string as the URL of the remote service.

  • If the value is an Object the properties listed below will be recognized.

The available options that can be passed via the Object parameter are:

  • contentType: The content-type HTTP header sent to the server. Default is "application/x-www-form-urlencoded".

  • data: Additional parameters which are sent to the remote service.

  • dataType: The type of result expected from the server. Commonly used values are "json" and "xml". Make sure you set this property for all requests to external data sources.

  • type:The type of request to make ("POST", "GET", "PUT" or "DELETE"), default is "GET".

  • url: The URL to which the request is sent. If set to function the data source will invoke it and use the result as the URL.

For more information on the above options, check the jQuery.ajax() documentation.

A sample definition of a DataSource configured for editing is shown below:

Configuring Editing Preferences Copy imageCopy
var dataSource = new Telerik.Data.DataSource({
    transport: {
        read: "http://myapi.mysite.com/Products",
        update: {
            url: "http://myapi.mysite.com/Products/Update",
            type: "POST"
        },
        destroy: {
            url: "http://myapi.mysite.com/Products/Destroy",
            type: "POST"
        },
        create: {
            url: "http://myapi.mysite.com/Products/Create",
            type: "POST"
        }
    }
});

Configure the Data Model

For the DataSource to work correctly with data items, distinguish them and treat their fields with accordance to their actual data type, you need to define a model. This is done through the schema.model property. The available options are:

  • id: The name of the field which acts as an identifier of the model. The field is expected to have unique values. The id must be set in order for automatic editing capabilities in Telerik controls like RadGrid to work.

  • fields: A set of key/value pairs that configure the model fields. The key specifies the name of the field. Quote the key if it contains spaces or other symbols which are not valid for a JavaScript identifier.

  • defaultValue: Specifies the value which will be used for the field when a new model instance is created. Default settings depend on the type of the field. Default for "string" is "", for "number" is 0 and for "date" is new Date() (today).

  • editable: Specifies if the field is editable or not. The default value is true.

  • nullable: Specifies if the defaultValue setting should be used. The default is false.

  • parse: Specifies the function which will parse the field value. If not set default parsers will be used.

  • type: Specifies the the type of the field. The available options are "string", "number", "boolean", "date". The default is "string".

  • validation: Specifies the validation options which will be used.

An example model declaration is shown below (For the purposes of the example, the transport property setup is omitted):

Configuring Editing Model Copy imageCopy
var dataSource = new Telerik.Data.DataSource({
    //..
    schema: {
        model: {
            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} default is String
                    type: "number",
                    // used when new model is created
                    defaultValue: 42,
                    validation: {
                        required: true,
                        min: 1
                    }
                }
            }
        }
    }
});
Caution

The transport, schema and type properties used with binding to remote data can be specified during initialization only. They are read-only after the DataSource instance is created.