编程语言
首页 > 编程语言> > javascript-Knockout.js:使用Repository-Pattern绑定到Click事件上的函数

javascript-Knockout.js:使用Repository-Pattern绑定到Click事件上的函数

作者:互联网

我在尝试在Knockout.js中实现Repository-Pattern时遇到了麻烦.
我发现很难处理click事件,因为:

问题:

>单击:不调用pendDeleteItem.我找不到范围;(
>在PendDeleteItem中,我有一个此问题.我需要进入PendingItem属性.

工作提琴:http://jsfiddle.net/ThomasDeutsch/j7Qxh/8/

目标:

单击后,该项目将发送到PendingItem.

限制:如果可能的话,我想保留ko.applyBindings(ViewModel),因为我想添加更多的Repositoris并在html中定义数据绑定,例如:customer.pendDeleteItem

解决方法:

问题的第一部分很简单.查看按钮的标记:

<button data-bind"click: $root.customer.pendDeleteItem "> sendTo -> PendingItems</button>

数据绑定属性名称后缺少=.更改为此:

<button data-bind="click: $root.customer.pendDeleteItem "> sendTo -> PendingItems</button>

下一个问题是单击处理程序中的此引用是“项目”,而不是视图模型.您将需要更改以下行:

this.PendingItems.push(item);
this.Items.remove(item);

要引用您的视图模型:

ViewModel.customer.PendingItems.push(item);
ViewModel.customer.Items.remove(item);

这是updated fiddle.

标签:repository-pattern,binding,knockout-js,javascript
来源: https://codeday.me/bug/20191201/2079287.html