我正在使用 RPNiemeyer 的剑道淘汰库。我有一个剑道网格,里面有剑道模板。在模板中,有一个按钮使用剔除单击绑定,该按钮调用更改 viewModel 的方法。重现步骤:
- 单击网格中的按钮。
- 调用一个方法来更改 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());
http://jsfiddle.net/hXn7e/25/