javascript - knockout .js : Binding to a function on the click-event using the Repository-Pattern

标签 javascript binding knockout.js repository-pattern

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

问题:

  1. 单击时:未调用 pendDeleteItem。我找不到范围 ;(
  2. 在 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/

相关文章:

knockout.js - 每页多个 View 模型和异步加载模板

javascript - 如何使我的底部弹出栏可关闭?

javascript - React 在页面刷新时无法正确渲染

javascript - 尝试失去范围

ruby - 在 Ruby 1.9 中使用 eval() 测试 Ruby 代码片段

javascript - knockout : Ajax request abort

javascript - 通过 css 绑定(bind)、knockout.js 组合动态和静态类

javascript - 在 Express 中手动设置 session ID

wpf - 如何为用户控件创建 MVVM 而不将所有内部结构暴露给消费者?

c# - 将 WPF DataGrid 绑定(bind)到 DataTable