悟空博客 发表于 2012-12-22 21:25:15

Knockoutjs实战开发:创建自定义绑定(Creating custom bindings)

<div id="cnblogs_post_body">在使用Knockoutjs时我们并不仅仅限于使用内建的绑定,比如:click、value等,我们可以创建自己的绑定规则。Knockoutjs为我们提供了非常灵活的支持,能够让我们自己去处理非常复杂的业务并且构造出可重用性的绑定。例如我们可以创建交互式的组件,比如表格、网格等。下面我们就来看看怎么一步步创建自定义绑定。
一、等级我们的绑定(Registering your binding)
我们可以通过ko.bindingHandlers来进行绑定。
<div class="cnblogs_code"> 1 ko.bindingHandlers.yourBindingName = { 2     init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { 3       // This will be called when the binding is first applied to an element 4       // Set up any initial state, event handlers, etc. here 5     }, 6     update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { 7       // This will be called once when the binding is first applied to an element, 8       // and again whenever the associated observable changes value. 9       // Update the DOM element based on the supplied values here. 10     } 11 };
页: [1]
查看完整版本: Knockoutjs实战开发:创建自定义绑定(Creating custom bindings)