Model
The Model object is from the namespace kendo.data.Model
and inherits from Kendo's ObservableObject
. It provides a known structure, or model, to the data that is used by a DataSource and can also be used to enable some more advanced functionality such as change tracking.
The Model object will specify a client-side model structure that can describe the data in terms of type and validation rules.
To create a new model, you must do so through the method
```javascript
// define a Model
var Service = kendo.data.Model.define( {
id: "serviceId", // the identifier of the model
fields: {
"serviceName": { // Property name for a field
type: "string", // "string"(default), "number", "boolean", or "date"
defaultValue: "Inspection", // Default value for field when model is created
editable: true, // Specifies whether field is editable
nullable: false, // Specifies if default value should be used when empty
parse: function(){...}, // Specifies custom parser for field value
validation: {...} // Specifies the options used by Kendo
// Validator such as 'required', 'min', and 'max'.
},
"unitPrice": {
type: "number"
},
"serviceId": {
type: "number"
}
}
});
// create an instance
var currentService = new Service( {
serviceName: "Rotate Tires",
unitPrice: 29.95,
serviceId: 400
});
/*
Since the properties within a model are observable, you need to use
special getter and setter methods to properly trigger the behaviors
that other functions and objects are observing. To retrieve the current value of one of these properties, use model_name.get()
such as currentService.get('unitPrice').
To set the value of the property and thereby change it, use
model_name.set() such as currentService.set('unitPrice', 14.95).
The concept of observable objects is a key feature of the MVVM framework
*/
console.log(currentService.get("serviceName")); // outputs "Rotate Tires"
console.log(currentService.get("unitPrice")); // outputs 29.95