Introduction
Knockout is a JavaScript library that helps you to create rich, responsive display and editor user interfaces with a clean underlying data model. Any time you have sections of UI that update dynamically (e.g., changing depending on the user鈥檚 actions or when an external data source changes), KO can help you implement it more simply and maintainably.
Headline features:
- Elegant dependency tracking - automatically updates the right parts of your UI whenever your data model changes.
- Declarative bindings - a simple and obvious way to connect parts of your UI to your data model. You can construct a complex dynamic UIs easily using arbitrarily nested binding contexts.
- Trivially extensible - implement custom behaviors as new declarative bindings for easy reuse in just a few lines of code.
Additional benefits:
- Pure JavaScript library - works with any server or client-side technology
- Can be added on top of your existing web application without requiring major architectural changes
- Compact - around 13kb after gzipping
- Works on any mainstream browser (IE 6+, Firefox 2+, Chrome, Safari, Edge, others)
- Comprehensive suite of specifications (developed BDD-style) means its correct functioning can easily be verified on new browsers and platforms
Developers familiar with Ruby on Rails, ASP.NET MVC, or other MV* technologies may see MVVM as a real-time form of MVC with declarative syntax. In another sense, you can think of KO as a general way to make UIs for editing JSON data鈥� whatever works for you :)
OK, how do you use it?
The quickest and most fun way to get started is by working through the interactive tutorials. Once you鈥檝e got to grips with the basics, explore the live examples and then have a go with it in your own project.
Is KO intended to compete with jQuery (or Prototype, etc.) or work with it?
Everyone loves jQuery! It鈥檚 an outstanding replacement for the clunky, inconsistent DOM API we had to put up with in the past. jQuery is an excellent low-level way to manipulate elements and event handlers in a web page. KO solves a different problem.
As soon as your UI gets nontrivial and has a few overlapping behaviors, things can get tricky and expensive to maintain if you only use jQuery. Consider an example: you鈥檙e displaying a list of items, stating the number of items in that list, and want to enable an 鈥楢dd鈥� button only when there are fewer than 5 items. jQuery doesn鈥檛 have a concept of an underlying data model, so to get the number of items you have to infer it from the number of TRs in a table or the number of DIVs with a certain CSS class. Maybe the number of items is displayed in some SPAN, and you have to remember to update that SPAN鈥檚 text when the user adds an item. You also must remember to disable the 鈥楢dd鈥� button when the number of TRs is 5. Later, you鈥檙e asked also to implement a 鈥楧elete鈥� button and you have to figure out which DOM elements to change whenever it鈥檚 clicked.
How is Knockout different?
It鈥檚 much easier with KO. It lets you scale up in complexity without fear of introducing inconsistencies. Just represent your items as a JavaScript array, and then use a foreach binding to transform this array into a TABLE or set of DIVs. Whenever the array changes, the UI changes to match (you don鈥檛 have to figure out how to inject new TRs or where to inject them). The rest of the UI stays in sync. For example, you can declaratively bind a SPAN to display the number of items as follows:
There are <span data-bind="text: myItems().length"></span> items
That鈥檚 it! You don鈥檛 have to write code to update it; it updates on its own when the myItems array changes. Similarly, to make the 鈥楢dd鈥� button enable or disable depending on the number of items, just write:
<button data-bind="enable: myItems().length < 5">Add</button>
Later, when you鈥檙e asked to implement the 鈥楧elete鈥� functionality, you don鈥檛 have to figure out what bits of the UI it has to interact with; you just make it alter the underlying data model.
To summarise: KO doesn鈥檛 compete with jQuery or similar low-level DOM APIs. KO provides a complementary, high-level way to link a data model to a UI. KO itself doesn鈥檛 depend on jQuery, but you can certainly use jQuery at the same time, and indeed that鈥檚 often useful if you want things like animated transitions.