注意:这是一种高级技术,通常仅在创建可重用绑定的库时使用。

默认情况下,绑定仅影响它们应用到的元素。 但是如果你想影响所有的后代元素呢?

为此,只需从绑定的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 false
var 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 elements
var innerBindingContext = bindingContext.extend(valueAccessor);
ko.applyBindingsToDescendants(innerBindingContext, element); // Also tell KO *not* to bind the descendants itself, otherwise they will be bound twice
return { 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 elements
var childBindingContext = bindingContext.createChildContext(
bindingContext.$rawData,
null, // Optionally, pass a string here as an alias for the data item in descendant contexts
function(context) {
ko.utils.extend(context, valueAccessor());
});
ko.applyBindingsToDescendants(childBindingContext, element); // Also tell KO *not* to bind the descendants itself, otherwise they will be bound twice
return { 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 forgotten
that the outer display mode is <span data-bind="text: $parentContext.displayMode"></span>.
</div>
</div>

通过修改绑定上下文和控制后代绑定,一个强大的和高级的工具来创建自己的自定义绑定机制。

最新文章

  1. Linux shell脚本编程(二)
  2. linux查看系统版本和系统位数
  3. HTML5 Geolocation 构建基于地理位置的 Web 应用
  4. 取url的键值对,location的search:从?开始的字符串
  5. Java内存模型---并发编程网 - ifeve.com
  6. Effiective C++ (一)
  7. 利用Qt制作一个helloworld
  8. 数据结构(主席树):HZOI 2016 采花
  9. linux设置语言编码
  10. [转]iOS开发使用半透明模糊效果方法整理
  11. LFS,编译自己的Linux系统 - 准备分区
  12. Linux使用技巧9--用dpkg管理你的软件
  13. DELPHI 任务栏无EXE显示
  14. ImageMagick wrapper for php
  15. 初探java对象比较
  16. 20180831-Linux环境下Python 3.6.6 的安装说明
  17. BZOJ3655 : 神经错乱数
  18. redis和mongodb的比较
  19. hdu 2795 公告板 (单点最值)
  20. SQL Server 损坏修复 之一 常见错误解读

热门文章

  1. member template
  2. Eclipse插件收集
  3. sacc scss less
  4. 「2014-4-13」Think twice before starting the adventure
  5. [Leetcode][JAVA] Minimum Depth of Binary Tree &amp;&amp; Balanced Binary Tree &amp;&amp; Maximum Depth of Binary Tree
  6. mysql5.7中文乱码问题的解决,将编码统一改成utf8的方法
  7. java 上传POJO model jar
  8. Wix 安装部署教程(十一) ---QuickWix
  9. Visual Studio 2012 Visual C++ 入门
  10. session机制详解以及session的相关应用