我正在使用 RPNiemeyer 的 kendo-knockout 库。我有一个剑道网格,里面有一个剑道模板。在模板中有一个使用敲除单击绑定的按钮,该按钮调用更改视图模型的方法。重现步骤:
- 单击网格中的按钮。
- 调用一个方法来更改 viewModel 的属性并提醒新值。
- 再次单击该按钮。该按钮不再起作用。
注意:如果您删除更改视图模型属性的行,则一切正常。
请解释这不起作用的原因,任何想法和解决方案将不胜感激。谢谢你!
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());