Knockout Introduction

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Knockout : Introduction https://knockoutjs.com/documentation/introduction.

html

Home Download / Install Tutorials Live examples Documentation Forum Source

Getting started Introduction


1. How KO works and what Knockout is a JavaScript library that helps you to create rich, responsive display and editor user
benefits it brings interfaces with a clean underlying data model. Any time you have sections of UI that update
2. Downloading and installing
dynamically (e.g., changing depending on the user’s actions or when an external data source
changes), KO can help you implement it more simply and maintainably.
Observables
1. Creating view models with Headline features:
observables
2. Working with observable arrays • Elegant dependency tracking - automatically updates the right parts of your UI whenever your
data model changes.
Computed observables
• Declarative bindings - a simple and obvious way to connect parts of your UI to your data
1. Using computed observables
model. You can construct a complex dynamic UIs easily using arbitrarily nested binding
2. Writable computed observables
3. How dependency tracking works contexts.
4. Pure computed observables • Trivially extensible - implement custom behaviors as new declarative bindings for easy reuse
5. Reference in just a few lines of code.

Bindings
Additional benefits:
Controlling text and appearance
1. The visible and hidden • Pure JavaScript library - works with any server or client-side technology
bindings • Can be added on top of your existing web application without requiring major architectural
2. The text binding changes
3. The html binding
• Compact - around 13kb after gzipping
4. The class and css bindings
• Works on any mainstream browser (IE 6+, Firefox 2+, Chrome, Safari, Edge, others)
5. The style binding
6. The attr binding • Comprehensive suite of specifications (developed BDD-style) means its correct functioning
can easily be verified on new browsers and platforms
Control flow
1. The foreach binding
Developers familiar with Ruby on Rails, ASP.NET MVC, or other MV* technologies may see MVVM
2. The if and ifnot bindings
3. The with and using bindings as a real-time form of MVC with declarative syntax. In another sense, you can think of KO as a
4. The let binding general way to make UIs for editing JSON data… whatever works for you :)
5. The component binding
6. Binding lifecycle events OK, how do you use it?
Working with form fields The quickest and most fun way to get started is by working through the interactive tutorials. Once
1. The click binding you’ve got to grips with the basics, explore the live examples and then have a go with it in your own
2. The event binding
project.
3. The submit binding
4. The enable and disable
bindings Is KO intended to compete with jQuery (or Prototype, etc.) or work
5. The value binding with it?
6. The textInput binding Everyone loves jQuery! It’s an outstanding replacement for the clunky, inconsistent DOM API we had
7. The hasFocus binding
to put up with in the past. jQuery is an excellent low-level way to manipulate elements and event
8. The checked binding
handlers in a web page. KO solves a different problem.
9. The options binding
10. The selectedOptions binding As soon as your UI gets nontrivial and has a few overlapping behaviors, things can get tricky and
11. The uniqueName binding
expensive to maintain if you only use jQuery. Consider an example: you’re displaying a list of items,
Rendering templates stating the number of items in that list, and want to enable an ‘Add’ button only when there are fewer
1. The template binding than 5 items. jQuery doesn’t have a concept of an underlying data model, so to get the number of

Binding syntax items you have to infer it from the number of TRs in a table or the number of DIVs with a certain CSS

1. The data-bind syntax class. Maybe the number of items is displayed in some SPAN, and you have to remember to update
2. The binding context that SPAN’s text when the user adds an item. You also must remember to disable the ‘Add’ button

1 of 2 12/30/2022, 7:30 PM
Knockout : Introduction https://knockoutjs.com/documentation/introduction.html

Creating custom bindings when the number of TRs is 5. Later, you’re asked also to implement a ‘Delete’ button and you have
1. Creating custom bindings to figure out which DOM elements to change whenever it’s clicked.
2. Controlling descendant bindings
3. Supporting virtual elements How is Knockout different?
4. Custom disposal logic
5. Preprocessing: Extending the It’s much easier with KO. It lets you scale up in complexity without fear of introducing
binding syntax 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
Components match (you don’t have to figure out how to inject new TRs or where to inject them). The rest of the UI
1. Overview: What components stays in sync. For example, you can declaratively bind a SPAN to display the number of items as
and custom elements offer follows:
2. Defining and registering
components
3. The component binding There are <span data-bind="text: myItems().length"></span> items
4. Using custom elements
5. Advanced: Custom component
loaders That’s it! You don’t have to write code to update it; it updates on its own when the myItems array
changes. Similarly, to make the ‘Add’ button enable or disable depending on the number of items,
Further techniques just write:
1. Loading and saving JSON data
2. Extending observables
3. Deferred updates <button data-bind="enable: myItems().length < 5">Add</button>
4. Rate-limiting observables
5. Unobtrusive event handling
6. Using fn to add custom Later, when you’re asked to implement the ‘Delete’ functionality, you don’t have to figure out what
functions bits of the UI it has to interact with; you just make it alter the underlying data model.
7. Microtasks
To summarise: KO doesn’t compete with jQuery or similar low-level DOM APIs. KO provides a
8. Asynchronous error handling
complementary, high-level way to link a data model to a UI. KO itself doesn’t depend on jQuery, but
Plugins you can certainly use jQuery at the same time, and indeed that’s often useful if you want things like
1. The mapping plugin animated transitions.

More information
1. Browser support
2. Getting help
3. Links to tutorials & examples
4. Usage with AMD using
RequireJs (Asynchronous
Module Definition)

Home Download / Install Tutorials Live examples Documentation Forum Source

(c) knockoutjs.com

2 of 2 12/30/2022, 7:30 PM

You might also like