Hi:
I am trying to convert the Knockout tutorial #2 to Kendo MVVM. Learn knockout Tutorial Collections
Not doing well.
Problem is the binding is not happening and the dropdown loses it's dropdownlist on add or remove.
Note: Kendo does not seem to have the calculated (logical) binding like Knockout does for visible and enabled.
Phil
I am trying to convert the Knockout tutorial #2 to Kendo MVVM. Learn knockout Tutorial Collections
Not doing well.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Tutorial2_4.aspx.cs" Inherits="KnockoutApp.Tutorials.Tutorial2_3" %><asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"><style type="text/css"> .deleteLink { color: red; font-weight: bold; }</style></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <!-- --> <div id="mvvmRegion"> <h2>Your seat reservations (<span data-bind="text: seats.length" ></span>)</h2> <button id="addSeatButton" data-bind="click: addSeat">Reserve another seat</button> <div style="width: 600px;"> <table id="reservationTable"> <thead><tr> <th data-field="name" style="width: 160px;">Passenger name</th> <th data-field="meal.mealName" style="width: 160px;">Meal</th> <th data-field="meal.price" style="width: 100px;">Surcharge</th> <th> </th> </tr></thead> <tbody> </tbody> </table> </div> <h3 data-bind="visible: surcharge"> Total surcharge: $<span data-bind="text: totalSurcharge00"></span> </h3> </div><script id="reservationTemplate" type="text/x-kendo-template"><tr> <td style="width: 160px;"><input id="nameTextBox" value="#= name #" data-bind="value: seats.name" /></td> <td style="width: 160px;"><input id="mealDropDown" class="mealDropDownClass" value="#= meal.mealName #" data-bind="value: seats.meal.mealName" /></td> <td style="width: 100px;">#= meal.price #</td> <td><a href="" class="deleteLink">Remove</a></td></tr></script> <script type="text/javascript"> // Class to represent a row in the seat reservations grid function SeatReservation(fname, initialMeal) { var self = this; self.name = fname; self.meal = initialMeal; } // var availableMeals = [ { mealName: "Standard (sandwich)", price: 0.0 }, { mealName: "Premium (lobster)", price: 34.95 }, { mealName: "Ultimate (whole zebra)", price: 290.0 } ]; // var viewModel = kendo.observable({ // type array populates the drop down meals: availableMeals, // array will hold the grid values seats: [ new SeatReservation("Steve", availableMeals[1]), new SeatReservation("Bert", availableMeals[0]) ], // Computed data totalSurcharge: function () { var total = 0; for (var i = 0; i < this.get("seats").length; i++) total += this.get("seats")[i].meal.price; return total + 0.00; }, totalSurcharge00: function () { var total = this.totalSurcharge() + 0.00; return total.toFixed(2); }, surcharge: function () { var total = this.totalSurcharge() + 0.00; if (total > 0) return true; return false; }, // maxSeating: function () { if (this.seats.length < 5) return true; return false; }, // seats.length < 5 // event execute on click of add button addSeat: function (e) { // add the items to the array of expenses this.get("seats").push(new SeatReservation("", self.availableMeals[1])); return false; }, removeSeat: function (seat) { var seats = this.get("seats"); seats.splice(seats.indexOf(seat), 1); } }); // apply the bindings kendo.bind($("#mvvmRegion"), viewModel); // var reservationDataSource = new kendo.data.DataSource({ data: viewModel.get("seats"), schema: { model: { id: "name", fields: { // data type of the field {Number|String|Boolean|Date} default is String name: { type: "String" }, meal: { mealName: { type: "String" }, price: { type: "Numeric" } } } } } }); // $("#reservationTable").kendoGrid({ dataSource: reservationDataSource, rowTemplate: kendo.template($("#reservationTemplate").html()), dataBound: function (e) { $(".deleteLink").click(function (e) { e.preventDefault(); if (confirm("Do you what to delete this?")) { var grid = $("#reservationTable").data("kendoGrid"); var dataItem = grid.dataItem($(this).closest("tr")); viewModel.removeSeat(dataItem); } return false; }); } }); $(".mealDropDownClass").kendoDropDownList({ dataTextField: "mealName", dataValueField: "mealName", dataSource: availableMeals }); // </script></asp:Content>Problem is the binding is not happening and the dropdown loses it's dropdownlist on add or remove.
Note: Kendo does not seem to have the calculated (logical) binding like Knockout does for visible and enabled.
Phil