Knockout.JS 2.1 现在已经发布了! 源代码可以在这里下载here. 这个版本主要是对2.0版本进行了一些bug修复,以及性能优化。
下面是主要的新特性:
- CommonJS and AMD support 【支持异步定义组件,以及配合requireJS异步加载viewModel、进行绑定】
- Great performance improvements to templating - use cloned DOM nodes when possible rather than parsing strings into elements. 【模板的性能优化:尽可能的使用复制DOM节点的方式而不是往DOM中插入文档标记的方式来渲染模板】
- Support for custom container-less bindings 【支持编写能够识别虚拟节点的自定义绑定(也就是支持<!—ko: yourBindName -->标记)】
- Ability to extend the binding context in a custom binding 【支持对自定义binding的绑定上下文进行扩充,比如:在父元素的绑定可以应用到下面所有的子元素!】
- Computed observables no longer can recursively evaluate themselves【computed属性不再会递归的修改自身】
$index
is available inforeach
scenarios from the binding context. There is no longer a need for workarounds like storing an index as a property of your data items. Note that$index
is an observable, so it needs to be reference using$index()
in expressions. 【这个非常有用,在foreach绑定的时候直接可以使用$index来绑定一个序号,并且这个变量是observable的,也就是说当集合元素数量改变的时候,index会自动更新】$parentContext
is available from the binding context as well. While,$parent
contains the actual data from one scope level up,$parentContext
contains the actual binding context ($data
,$parent
,$parents
,$root
, etc.) of the parent. 【$parentContext
属性被加入到绑定上下文,这个属性里包含其他绑定上下文对象:parent,root等】ko.isComputed
is a helper function that was added to determine if a value is a computed observable or not. 【添加了一个helper方法,用于判断一个属性是不是computed属性】ko.toJSON
now passes its arguments on toJSON.stringify
after callingko.toJS
. This makes creating a handy “debug” section even easier<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>
and this can be used to include only certain properties in the converted JSON likeko.toJSON(person, ['firstName', 'lastName'])
. This seems even more flexible in many cases than the approach that I mentioned here. 【ko.toJSON方法现在可以传入几个参数,来控制只序列化model中的某几个属性,这使得程序调试更加方便了】- Better support for using a single instance of KO between documents - you can now pass an instance of KO between documents and have bindings work properly across the documents. Previously the computed observables associated with bindings would dispose themselves when outside of the original document as they believed they were no longer part of the DOM. 【对多个document共享一个viewModel实例提供了更好的支持,现在可以在多个文档之间传递viewModel对象,并使他们生效。】
- Numerous other bug and performance fixes 【一些其他bug的修复和性能的优化】
如有不妥的地方,欢迎拍砖!