Kendo UI MVVM and the Grid: Example 02

Introduction to binding an editable Grid to a ViewModel with a calculated field

HTML

<link rel="stylesheet" href="http://cdn.kendostatic.com/2012.2.710/styles/kendo.common.min.css">
<link rel="stylesheet" href="http://cdn.kendostatic.com/2012.2.710/styles/kendo.default.min.css">
<script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
<div id="people">
    <div data-role="grid" 
         data-bind="source: people" 
         data-editable="true" 
         data-columns='[{"field": "firstName", "title": "First Name"}, 
                        {"field": "lastName", "title": "Last Name"}, 
                        {"field": "lastUpdated", "title": "Full Name", "template": "#= fullName() #"}]'>
    </div>
</div>

JavaScript

var Person = kendo.data.Model.define({
    fields: {
        "firstName": {
            type: "string"
        },
        "lastName": {
            type: "string"
        },
        "lastUpdated": {
            type: "date",
            editable: false
        }
    },
    fullName : function() {
        return this.get("firstName") + " " + this.get("lastName");
    }
});

// Define a DataSource
var peopleDataSource = new kendo.data.DataSource({
    data: [
        { id: 1, firstName: "John", lastName: "DeVight" },
        { id: 2, firstName: "Wendy", lastName: "Parry" }
    ],
    schema: {
        model: Person
    }
});

peopleDataSource.bind("change", function(e) {
    if (e.action === "itemchange") {
        if (e.field === "firstName" || e.field === "lastName") {
        
            //e.items[0].dirty = true;
            //e.items[0].set('lastUpdated', new Date());
            console.log(e.items[0]);
            kendo.data.ObservableObject.fn.set.call(e.items[0], "lastUpdated", new Date());
        }
    }
});

// Create the ViewModel.
var vm = kendo.observable({
    people: peopleDataSource
});

kendo.bind($("#people"), vm);