O'Reilly logo

Mastering KnockoutJS by Timothy Moran

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Knockout Switch-Case

Despite being a plugin that targets a single, specific use case, Knockout Switch-Case's popularity on GitHub is evidence that a switch/case control flow binding is a very useful tool. Instead of writing out a series of if/ifnot bindings, a single case-switch binding can be used:

<div data-bind="switch: orderStatus"> <div data-bind="case: 'shipped'"> Your order has been shipped. Your tracking number is <span data-bind="text: trackingNumber"></span>. </div> <div data-bind="case: 'pending'"> Your order is being processed. Please be patient. </div> <div data-bind="case: 'incomplete'"> Your order could not be processed. Please go back and complete the missing data. </div> <div data-bind="case: $default"> Please call customer service ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required