foreach指令,包含as选项的时候,不再创建循环内部的上下文,意味着循环内部访问外部属性无需$parent
// old version
<div data-bind="foreach:{data: list, as: 'item'}">
<span data-bind="text: item.name"> </span>
<span data-bind="text: $parent.outterAttr"></span>
</div>
// new version
<div data-bind="foreach:{data: list, as: 'item'}">
<span data-bind="text: item.name"> </span>
<!-- 无需使用$parent,context和foreach外部一致 -->
<span data-bind="text:outterAttr"></span>
</div>
可以通过设置ko.options.createChildContextWithAs = true 来适配原有的代码(默认是false,不改代码的前提下版本升级适配)
相当于生命周期里的afterrender方法 会在内部render完之后调用,koDescendantsComplete(组件内部调用)会等子组件全部渲染完之后调用
<div data-bind="childrenComplete:function(){}">
</div>
// 组件初始化方法
function init () {
this.koDescendantsComplete = function () {
// 组件初始化完调用,包含子组件
}
}
<div data-bind="html:`<div>
支持反引号了
</div>`"></div>
和array的sort和revers方法不同,此方法无副作用,仅仅是返回数据的拷贝
ko终于良心发现添加了,可以绑定多个类名
<div data-bind="class:classes"></div>
和官网原来的指令例子withPropertise一致,减少$parent和$parentContext的使用(划重点很实用)
<div data-bind="let:{outterArr: outterAttr}">
<parentmodal >
// 内部可以直接使用outterArr
// 无需通过params传参或者$parent获取
</parentmodal>
</div>
可以查看相应的属性依赖
可用于判断对象是否是ObservableArray
解读:状态切换再也不用 一个visible:flag 另一个 visible:!flag()这么写了
类似于with但效率更高
字符串模板编译时添加缓存,不做更多描述
用于执行一些只运行一次的方法
默认使用jQuery,可以使用background-color这样的标准样式名称(原来需要写成backgrounColor)
如果出现 "<-- /ko -->" 标签不匹配, ko现在会报错
Observables notify a new spectate event whenever their value changes. Unlike the standard change event, this new event isn't necessarily delayed by rate-limiting or deferred updates. You can subscribe to the event without waking a sleeping pure computed; the computed will notify the event if it is accessed with a new value. 尚未搞清楚实际应用场景,待我翻翻issue后续更新