Kendo UI MVVM Sample: Example 02

Example of using the Kendo UI MVVM to bind a model to simple html elements. Also demonstrates the use of calculated fields.

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 class="title">Instructions</div>
<div>
Change the "First Name" or "Last Name" and see the "Full Name" change.
</div>

<div id="personFields">
    <div class="field">
        <div>First Name:</div><input data-bind="value: firstName"/>
    </div>
    <div class="field">
        <div>Last Name:</div><input data-bind="value: lastName"/>
    </div>
    <div class="field">
        <div>Full Name:</div><input data-bind="value: fullName"/>
    </div>
</div>

CSS

.title {
    font-weight: bold;
    font-size: 18px;
}

#personFields {
    margin-top: 20px;
}

.field {
    display: block;
    width: 100%;
    padding-bottom: 5px;
}

.field div {
    display: inline-block;
    width: 100px;
}

JavaScript

// Create an observable view model object.
var person = kendo.observable({
    firstName: function(){
        return "Nagi";
    },
    lastName: "Ramadan",

    // Create a calculated field.
    fullName: function() {
        // The "get" function must be used so that changes to any
        // dependencies will be reflected in the calculated field.
        return this.get("firstName").value() + " " + this.get("lastName");
    }
});

// Bind the view model to the personFields element.
kendo.bind($('#personFields'), person);