在大多数状况下,数据绑定属性提供了一种干净和简洁的方式来绑定到视图模型。 然而,事件处理是一个经常会致使详细数据绑定属性的领域,由于匿名函数一般是传递参数的推荐技术。 例如:html
<a href="#" data-bind="click: function() { viewModel.items.remove($data); }"> remove </a>
做为替代,Knockout提供了两个帮助函数,它们容许您标识与DOM元素关联的数据: app
ko.dataFor(element)
- 返回可用于与元素绑定的数据 ko.contextFor(element)
- 返回DOM元素可用的整个绑定上下文这些帮助函数能够在事件处理程序中使用,这些事件处理程序使用相似于jQuery的绑定或单击的方式地附加。 上面的函数能够链接到每一个连接一个删除类,如:函数
$(".remove").click(function () { viewModel.items.remove(ko.dataFor(this)); });
更好的是,这种技术能够用于支持事件委托。 jQuery live / delegate / on函数是一个简单的方法来实现这一点:this
$(".container").on("click", ".remove", function() { viewModel.items.remove(ko.dataFor(this)); });
如今,单个事件处理程序在更高级别附加,并处理与remove类的任何连接的点击。 此方法还具备自动处理动态添加到文档的附加连接(多是因为项目添加到observableArray的结果)的额外好处。spa
此示例显示父级和子级的多个级别上的“add”和“remove”连接,每一个类型的连接具备单个处理程序。code
UI源码:htm
<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>
视图模型源码:blog
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; });
不管嵌套连接如何嵌套,处理程序老是可以识别和操做适当的数据。 使用这种技术,咱们能够避免将处理程序附加到每一个连接的开销,并能够保持标记清洁和简洁。事件