KnockoutJS 3.X API 第五章 高级应用(2) 控制后代绑定
注意:这是一种高级技术,通常仅在创建可重用绑定的库时使用。
默认情况下,绑定仅影响它们应用到的元素。 但是如果你想影响所有的后代元素呢?
为此,只需从绑定的init函数中返回{controlsDescendantBindings:true}即可。
示例1:控制是否应用后代绑定
对于一个非常简单的例子,这里有一个名为allowBindings的自定义绑定,允许后代绑定仅当它的值为true时才应用。 如果值为false,则allowBindings告诉Knockout它负责后代绑定,因此它们不会像往常一样绑定。
ko.bindingHandlers.allowBindings = {init: function(elem, valueAccessor) {// Let bindings proceed as normal *only if* my value is falsevar shouldAllowBindings = ko.unwrap(valueAccessor());return { controlsDescendantBindings: !shouldAllowBindings };} };
要使此效果生效,以下是一个示例用法:
<div data-bind="allowBindings: true"><!-- This will display Replacement, because bindings are applied --><div data-bind="text: 'Replacement'">Original</div> </div><div data-bind="allowBindings: false"><!-- This will display Original, because bindings are not applied --><div data-bind="text: 'Replacement'">Original</div> </div>
示例2:为子孙绑定提供附加值
通常,使用controlsDescendantBindings的绑定也将调用ko.applyBindingsToDescendants(someBindingContext,element)来对一些修改的绑定上下文应用后代绑定。 例如,您可以使用一个名为withProperties的绑定将一些额外的属性附加到绑定上下文,然后可用于所有后代绑定:
ko.bindingHandlers.withProperties = {init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {// Make a modified binding context, with a extra properties, and apply it to descendant elementsvar innerBindingContext = bindingContext.extend(valueAccessor);ko.applyBindingsToDescendants(innerBindingContext, element);// Also tell KO *not* to bind the descendants itself, otherwise they will be bound twicereturn { controlsDescendantBindings: true };} };
正如你可以看到,绑定上下文有一个扩展函数,产生一个带有额外属性的克隆。 extend函数接受具有要复制的属性的对象或返回此类对象的函数。 函数语法是首选的,以便将来在绑定值中的更改始终在绑定上下文中更新。 此过程不会影响原始绑定上下文,因此不会影响同级元素的危险 - 它只会影响后代。
以下是使用上述自定义绑定的示例:
<div data-bind="withProperties: { emotion: 'happy' }">Today I feel <span data-bind="text: emotion"></span>. <!-- Displays: happy --> </div> <div data-bind="withProperties: { emotion: 'whimsical' }">Today I feel <span data-bind="text: emotion"></span>. <!-- Displays: whimsical --> </div>
示例3:在绑定上下文层次结构中添加额外的级别
绑定(如with和foreach)在绑定上下文层次结构中创建额外的级别。 这意味着它们的后代可以通过使用$ parent,$ parents,$ root或$ parentContext来访问外部级别的数据。
如果你想在自定义绑定中这样做,那么不使用bindingContext.extend(),使用bindingContext.createChildContext(someData)。 这返回一个新的绑定上下文,其viewmodel是someData,其$ parentContext是bindingContext。 如果需要,您可以使用ko.utils.extend扩展具有额外属性的子上下文。 例如,
ko.bindingHandlers.withProperties = {init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {// Make a modified binding context, with a extra properties, and apply it to descendant elementsvar childBindingContext = bindingContext.createChildContext(bindingContext.$rawData, null, // Optionally, pass a string here as an alias for the data item in descendant contextsfunction(context) {ko.utils.extend(context, valueAccessor());});ko.applyBindingsToDescendants(childBindingContext, element);// Also tell KO *not* to bind the descendants itself, otherwise they will be bound twicereturn { controlsDescendantBindings: true };} };
这个更新的withProperties绑定现在可以以嵌套方式使用,每个嵌套级别都可以通过$ parentContext访问父级别:
<div data-bind="withProperties: { displayMode: 'twoColumn' }">The outer display mode is <span data-bind="text: displayMode"></span>.<div data-bind="withProperties: { displayMode: 'doubleWidth' }">The inner display mode is <span data-bind="text: displayMode"></span>, but I haven't forgottenthat the outer display mode is <span data-bind="text: $parentContext.displayMode"></span>.</div> </div>
通过修改绑定上下文和控制后代绑定,一个强大的和高级的工具来创建自己的自定义绑定机制。
转载于:https://www.cnblogs.com/smallprogram/p/5968616.html
KnockoutJS 3.X API 第五章 高级应用(2) 控制后代绑定相关推荐
- 数据库系统概念总结:第五章 高级SQL
周末无事水文章,期末备考的总结资料 第五章 高级SQL 5.1 使用程序设计语言访问数据库 5.1.1 JDBC(Java DataBase Connectivity) JDBC标准定义了Java程序 ...
- knockoutjs ajax分页,KnockoutJS 3.X API 第四章之数据控制流foreach绑定
foreach绑定 foreach绑定主要用于循环展示监控数组属性中的每一个元素,一般用于table标签中 假设你有一个监控属性数组,每当您添加,删除或重新排序数组项时,绑定将有效地更新UI的DOM- ...
- 语言 重量计算_R语言 第五章 高级绘图工具(4)
直方图 实例:nutshell包的births2006.smpl数据集,包含了2006年美国出生人口的数据的10%样本,每一条记录有13个变量.使用数据集前,需通过install.packages(& ...
- KnockoutJS 3.X API 第四章 表单绑定(11) options绑定
目的 options绑定主要用于下拉列表中(即<select>元素)或多选列表(例如,<select size='6'>).此绑定不能与除<select>元素之外的 ...
- 2021.11.21【读书笔记】丨生物信息学与功能基因组学(第五章 高级数据库搜索 中 )
5.3 寻找远缘相关蛋白质:位置特异性迭代BLAST(PSI-BLAST)和DELTA-BLAST PAM250矩阵给探测远缘相关蛋白质提供了一个更好的打分系统,可以改变打分矩阵来检测远缘蛋白质,但仍 ...
- KnockoutJS 3.X API 第七章 其他技术(2) 使用扩展器来增加可观察量(监控属性)
Knockout observables提供了支持读取/写入值并在值改变时通知订阅者所需的基本功能. 但在某些情况下,您可能希望向可观察者添加其他功能. 这可能包括通过在可观察者前面放置一个可写的计算 ...
- 2021.12.19【读书笔记】丨生物信息学与功能基因组学(第五章 高级数据库搜索 下)
5.5 用类似于BLAST的比对工具快速搜索基因组DNA 需求:随着基因组DNA数据库数量增长,对比对工具要求越来越高 能在基因组DNA中找到外显子 比对时考虑基因组DNA包含的测序错误 有相应的算法 ...
- 第五章 高级搜索
5.1 搜索排序 public void seacher(String queryContion,intnum,Sort sort) {try {IndexSearcher searcher=new ...
- 第五章 STM32+LD3320语音识别控制淘宝USB宿舍书桌灯
目录 前言 一.设备准备 二.改造USB宿舍书桌灯 1.原理 2.将控灯板子的护壳拆开 3.在控灯板子供电端的GND焊上一根杜邦线 4.将每个触发脚焊上杜邦线 三.代码讲解 1.首先初始化一下控灯的G ...
最新文章
- 目标还是中国人,纽约智慧城市项目想通过EB-5募资10亿
- xml+dom4j+xpath学生管理系统
- HTML里Dom onload和jQuery document ready这两个事件的区别
- webpack4.0配置记录(2)
- Itemplate 自定义控件
- 比特币的服务器作用,比特币白皮书解读-时间戳服务器
- iapp退出软件按钮代码_还在为金蝶财务软件发愁吗?超详细!金蝶财务软件实操流程,速收...
- EViews9.0程序安装及注意事项
- treetable怎么带参数_jQuery.treetable使用及异步加载
- HTML——多选框和按钮、搜索框滑块简单验证
- Windows安全配置
- file api java_File的API和常用方法详解_动力节点Java学院整理
- 洛谷p3398仓鼠找suger题解
- 一个新手的评价---人机交互方面(有些不搭题
- 操作系统基本原理---设备管理
- [C/C++]#ifndef,#define用法
- Python异常和异常处理
- 《万里长江图》告诉我们:金沙江是长江的正源
- 连接linux服务器ip地址设置方法,linux下配置ip地址的方法
- 使用Power Apps 创建门户应用
热门文章
- mysql 出现 “1067 - Invalid default value for ‘UPDATE_TIME‘ “ 错误提示的解决办法
- SpringCloud Config详解
- java B2B2C 源码多租户电子商城系统-Spring Cloud整合Netflix Archaius介绍
- shelve模块简单用法
- 阿里安全体系获国际顶会表彰,安全技术将有九大新趋势
- 强一致、高可用、自动容灾能力背后,阿里X-Paxos的应用实践
- VC6.0设置注释快捷键
- 2018清华计算机类专业录取分数线,清华大学2018-2019年各省各专业录取分数线
- android 检测网络ftp,Android端与Android端利用WIFI进行FTP通信
- linux查看目录分区格式,Linux下查看分区的文件系统类型