I am working on a project that requires me to use Kendo UI with Knockout.js for a mobile application, since I use the knockout-kendo library to link these libraries, the application consists of a simple list of products with detailed views for each product and shopping cart, however, I am having some problems updating the number of items in my cart.
I use knockout-kendo bindings in my application as follows:
<div data-role="view" id="cart" data-title="Cart" data-layout="main-layout"> <div data-bind="if: items().length == 0">No items currently in cart</div> <ul data-role="listview" data-style="inset" data-bind="kendoListView: { data: items, template: cartTemplate }"></ul> </div>
where is the template:
<script type="text/x-kendo-template" id="cartListTemplate"> <div class="km-listview-link cart-item-container" data-id="#= Id #"> <div class="product-image"> <img src="#= ImageUrl #"> </div> <div class="product-description"> <p>#= Name #</p> <p>#= formattedPrice #</p> <p>#= quantity #</p> </div> <a data-role="button" data-icon="delete" class="km-primary" data-bind="click: removeItem">Delete</a> </div> </script>
and ViewModel:
CartViewModel : function () { var self = this; globalKo.cartItems = self.items = ko.observableArray(JSON.parse(localStorage.getItem('cart')) || []); self.cartTemplate = kendo.template($('#cartListTemplate').html()); self.removeItem = function (vm, event) { var element = $(event.target).parents('div.cart-item-container'); productId = element.data('id'); var cartItem = globalKo.cartItems().filter(function (element) { return element.Id == productId; })[0]; if (cartItem.quantity > 1) { cartItem.quantity --; } else { self.items.remove(cartItem); } app.saveCart(); self.items.valueHasMutated(); } }
All this works as intended, except that when the value of the array mutates (the valueHasMutated function valueHasMutated called or the array has an element added or deleted), suddenly the buttons stop being buttons and turn into plain text, they also work, as they stop calling the function to which they are attached. As you can see from the code fragments, click binding is done with markup and does not work properly.
It might be worth noting that I call the valueHasMutated function, because otherwise the view does not update the number of items in the cart.
To illustrate the problem, here are a few images:
Before pressing a button

After pressing the button

I donโt quite understand why this is happening, I assume that this has something to do with the Kendo user interface and not so much with knockout.js.
I also made a fiddle demonstrating the problem, you can find it here