我在尝试在 Knockout.js 中实现 Repository-Pattern 时遇到了困难。 我发现很难处理点击事件,因为:
问题:
- 单击时:未调用 pendDeleteItem。我找不到范围 ;(
- 在 PendDeleteItem 中我遇到了这个问题。我需要访问 PendingItem 属性。
工作 fiddle :http://jsfiddle.net/ThomasDeutsch/j7Qxh/8/
目标:
单击后,项目将发送到 PendingItem。
限制:如果可能,我想保留 ko.applyBindings(ViewModel),因为我想添加更多 Repositoris 并在 html 中定义数据绑定(bind),例如:customer.pendDeleteItem
最佳答案
问题的第一部分很简单。查看按钮的标记:
<button data-bind"click: $root.customer.pendDeleteItem "> sendTo -> PendingItems</button>
data-bind
属性名称后缺少 =
。将其更改为:
<button data-bind="click: $root.customer.pendDeleteItem "> sendTo -> PendingItems</button>
下一个问题是点击处理程序中的 this
指的是“项目”,而不是 View 模型。您将需要更改这些行:
this.PendingItems.push(item);
this.Items.remove(item);
引用你的 View 模型:
ViewModel.customer.PendingItems.push(item);
ViewModel.customer.Items.remove(item);
这是一个 updated fiddle .
关于javascript - knockout .js : Binding to a function on the click-event using the Repository-Pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10928436/