<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script type="text/javascript" src="../scripts/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../scripts/jquery.tmpl.min.js"></script>
<script type="text/javascript" src="../scripts/knockout-latest.js"></script>
<script type="text/javascript">
var viewModel = {
customers: ko.observableArray([]),
selectedCustomer: ko.observable(),
orders: ko.observableArray([]),
customerFirstName: ko.observable(),
order: function () {
return { itemName: "", description: "", unitPrice: 0, units: 0 }
},
customer: function () {
return { firstName: "", lastName: "", joinedDate: new Date(), orders: ko.observableArray([this.order()]) }
},
addOrder: function () {
this.orders.push(this.order());
},
addCustomer: function () {
this.customers.push(this.customer());
},
setSelectedCustomer: function (customer) {
this.selectedCustomer(customer);
}
};
ko.dependentObservable(function () {
if (this.selectedCustomer()) {
this.orders(this.selectedCustomer().orders());
}
}, viewModel);
</script>
</head>
<body>
<span data-bind="text: customerFirstName"></span>
<table>
<thead>
<tr>
<th>
First Name
</th>
<th>
Last Name
</th>
<th>
Joined Date
</th>
</tr>
</thead>
<tbody data-bind="template : {name: 'customersTemplate', foreach: customers}">
</tbody>
</table>
<button data-bind="click: function() {viewModel.addCustomer()}">
Add customer</button>
<table>
<tbody data-bind="template: {name: 'ordersTemplate', foreach: orders}">
</tbody>
</table>
<button data-bind="click: addOrder">
Add Order</button>
<script type="text/html" id="customersTemplate">
<tr data-bind="click: function() {viewModel.setSelectedCustomer($data)}">
<td>
<input type="text" data-bind="value: firstName" />
</td>
<td>
<input type="text" data-bind="value: lastName" />
</td>
<td>
<input type="datetime-local" data-bind="value: joinedDate"/>
</td>
</tr>
</script>
<script type="text/html" id="ordersTemplate">
<tr>
<td>
<input type="text" data-bind="value: itemName" />
</td>
<td>
<input type="text" data-bind="value: description" />
</td>
<td>
<input type="text" data-bind="value: unitPrice"/>
</td>
<td>
<input type="text" data-bind="value: units"/>
</td
</tr>
</script>
<script type="text/javascript">
ko.applyBindings(viewModel);
</script>
</body>
</html>