I am using RPNiemeyer`s kendo-knockout library. I have a kendo grid with a kendo template in it. In the template there is a button which uses knockout click binding which calls a method that changes the viewModel. Steps to reproduce:
- Click the button in the grid.
- A method is called that changes the property of the viewModel and alerts the new value.
- Click the button again. The button is not working any more.
Note: If You remove the line that changes the property of the viewmodel everything is working fine.
Please explain the reason why this is not working and any ideas and solutions will be greatly appreciated. Thank You!
html:
<div id="grid" data-bind="kendoGrid: { data: fruits, columns: [
{
field: 'name',
title: 'Fruit',
width: 50
},
{
template: '<button class=\'k-button\' data-bind=\'click: function() { changeFruit() }\' >Change Fruit Name</button>',
width: 30
}
],
scrollable: false, sortable: true, pageable: false }" style="height: 380px">
</div>
javascript:
var ViewModel = function() {
this.fruit1 = {
color: ko.observable("green"),
name: ko.observable("apple"),
};
this.fruit2 = {
color: ko.observable("orange"),
name: ko.observable("orange"),
};
this.fruits = ko.observableArray([
this.fruit1,
this.fruit2
]);
this.changeFruit = function() {
// this line breaks the binding,
// when You change the property of the viewModel
// You cannot call this function any more
this.fruits()[0].name("Test");
alert(this.fruits()[0].name());
}
};
ko.applyBindings(new ViewModel());
http://jsfiddle.net/hXn7e/25/