在大多数情况下,数据绑定属性提供了一种干净和简洁的方式来绑定到视图模型。 然而,事件处理是一个常常会导致详细数据绑定属性的领域,因为匿名函数通常是传递参数的推荐技术。 例如:

<a href="#" data-bind="click: function() { viewModel.items.remove($data); }">
remove
</a>

作为替代,Knockout提供了两个帮助函数,它们允许您标识与DOM元素关联的数据:

  • ko.dataFor(element) - 返回可用于与元素绑定的数据

  • ko.contextFor(element) - 返回DOM元素可用的整个绑定上下文

这些帮助函数可以在事件处理程序中使用,这些事件处理程序使用类似于jQuery的绑定或单击的方式地附加。 上面的函数可以连接到每个链接一个删除类,如:

$(".remove").click(function () {
viewModel.items.remove(ko.dataFor(this));
});

更好的是,这种技术可以用于支持事件委托。 jQuery live / delegate / on函数是一个简单的方法来实现这一点:

$(".container").on("click", ".remove", function() {
viewModel.items.remove(ko.dataFor(this));
});

现在,单个事件处理程序在更高级别附加,并处理与remove类的任何链接的点击。 此方法还具有自动处理动态添加到文档的附加链接(可能是由于项目添加到observableArray的结果)的额外好处。

示例:嵌套子节点

此示例显示父级和子级的多个级别上的“add”和“remove”链接,每个类型的链接具有单个处理程序。

UI源码:

<ul id="people" data-bind='template: { name: "personTmpl", foreach: people }'>
</ul> <script id="personTmpl" type="text/html">
<li>
<a class="remove" href="#"> x </a>
<span data-bind='text: name'></span>
<a class="add" href="#"> add child </a>
<ul data-bind='template: { name: "personTmpl", foreach: children }'></ul>
</li>
</script>

视图模型源码:

var Person = function(name, children) {
this.name = ko.observable(name);
this.children = ko.observableArray(children || []);
}; var PeopleModel = function() {
this.people = ko.observableArray([
new Person("Bob", [
new Person("Jan"),
new Person("Don", [
new Person("Ted"),
new Person("Ben", [
new Person("Joe", [
new Person("Ali"),
new Person("Ken")
])
]),
new Person("Doug")
])
]),
new Person("Ann", [
new Person("Eve"),
new Person("Hal")
])
]); this.addChild = function(name, parentArray) {
parentArray.push(new Person(name));
};
}; ko.applyBindings(new PeopleModel()); //attach event handlers
$("#people").on("click", ".remove", function() {
//retrieve the context
var context = ko.contextFor(this),
parentArray = context.$parent.people || context.$parent.children; //remove the data (context.$data) from the appropriate array on its parent (context.$parent)
parentArray.remove(context.$data); return false;
}); $("#people").on("click", ".add", function() {
//retrieve the context
var context = ko.contextFor(this),
childName = context.$data.name() + " child",
parentArray = context.$data.people || context.$data.children; //add a child to the appropriate parent, calling a method off of the main view model (context.$root)
context.$root.addChild(childName, parentArray); return false;
});

无论嵌套链接如何嵌套,处理程序总是能够识别和操作适当的数据。 使用这种技术,我们可以避免将处理程序附加到每个链接的开销,并可以保持标记清洁和简洁。

最新文章

  1. CentOS安装Nvidia显卡驱动提示Nouveau正在使用的问题
  2. java的nio之:java的nio系列教程之Scatter/Gather
  3. html &lt;input&gt;标签类型属性type(file、text、radio、hidden等)详细介绍
  4. websphere变成英文了
  5. 将 Callout 容器添加到移动设备应用程序中
  6. 在自学java路上遇上的南墙
  7. 《深入理解Java虚拟机》读书笔记-垃圾收集器与内存分配策略
  8. 初学者最易懂的git教程在这里!
  9. 将 Net 项目升级 Core项目经验:(三)迁移后的Net Standard版本的类库测试和多平台项目实测
  10. Vert.x系列(三)--ClusteredEventBus源码分析
  11. shell:实现linux服务器资源监控并发送告警邮件
  12. 连连看 (BFS)
  13. Struts2把数据封装到集合中之封装到map中
  14. LINUX 下Jexus部署ASP.NET Core WebApi
  15. JavaScript循环练习
  16. linux分区之gpt(大于2T的分区)
  17. ifconfig源码分析之与内核交互数据
  18. Linux:有趣的命令(更新)
  19. ruby安装devkit
  20. JSON-RPC(jsonrpc4j)使用demo

热门文章

  1. 转:简单窗体振动-WaitForSingleObject,消息,winapi
  2. Python 基础语法学习笔记
  3. install hdp 2.2 on ubuntu 14.04
  4. ARP报文发送的可视化实现
  5. WPF-开机自启
  6. firefox浏览器无法显示bootstrap图标问题总结
  7. centos 7.2 安装PHP7.1+apache2.4.23
  8. RunTimeException包括:
  9. .NET C#-- 利用BeginInvoke与EndInvoke完成异步委托方法并获取方法执行返回值示例
  10. USACO翻译:USACO 2012 JAN三题(1)