Just a quick note that Knockout 2.2 is now available. You can read more about the details of this release in the announcement or Steve Sanderson’s blog post.
Here are a few of my favorite changes:
- The
cssbinding can either toggle specific classes on and off like (css: { error: hasError, required: isRequired }) or now add one or more classes dynamically likecss: someClassOrClasses. This is handy when the class names are dynamic. Previously, you could do something similar with theattrbinding against the class attribute, but it would overwrite the class attribute completely. When using thecssbinding in this way, it will now preserve existing classes and will properly remove any class or classes that were previously added by the binding when the value changes. - The
ifandifnotbindings now only re-render their contents when the bound value changes between truthy and falsy. This helps to alleviate the performance issue described in the first half of this post. - There is a new
peekfunction available onobservablesandcomputed observables. This allows you to retrieve its value without creating a dependency. This definitely can be useful when you want tight control over when bindings and other subscriptions are triggered, but I would use caution as it does circumvent the normal dependency tracking mechanism. If you find that you need this functionality, then you probably have a good reason why you don’t want a dependency and would understand what does and doesn’t trigger updates. - The
foreachbinding and theforeachoption of thetemplatebinding now try to understand if items have been moved and will relocate the content rather than tearing it down and re-rendering it at its new position. This comes with some new callbacks (beforeMoveandafterMove). - The
kovariable is now available from within bindings, even ifkois not a global variable (common in AMD scenarios).
You can download the 2.2 files from here. There were no intentional breaking changes made in 2.2 (unless someone was relying on an existing bug), so if you have any issues with your application not behaving as it did in 2.1, please log an issue here.