Featured image of post Knockout.js In a Nutshell

Knockout.js In a Nutshell

MVVM In Javascript!

What the Heck is Knockout.js?

Knockout.js is a lightweight JavaScript library that brings the MVVM (Model-View-ViewModel) pattern to your web applications. In simpler terms, it helps you connect your data (Model) to your UI (View) with some magic (ViewModel).

You might be thinking, “But isn’t that what React and Vue do?” Yes. But Knockout was doing it before it was cool.

Why Should You Care?

1. Simplicity

Knockout doesn’t need Webpack, Babel, or a degree in astrophysics to get started. Just drop a single JavaScript file into your project, and boom—you’re binding data like a pro.

2. Two-Way Data Binding

No more manually updating your UI when data changes. Knockout.js keeps everything in sync. If you update your JavaScript object, your UI updates automatically. No need for document.getElementById nonsense.

3. No Dependencies

Unlike modern frameworks that need a 500MB node_modules folder, Knockout runs on vanilla JavaScript. No dependencies. Just plug and play.

4. It Still Works

Even though it’s old-school, Knockout is still a solid choice for applications that need data binding without the overhead of modern frameworks. It’s especially great for legacy projects or small apps.

The Basics: How Knockout.js Works

Alright, enough chit-chat. Let’s see Knockout in action.

Step 1: Include Knockout

You can include Knockout.js in your project with a simple <script> tag:

1
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-min.js"></script>

No npm install, no yarn add, no webpack.config.js headaches. Just old-school JavaScript.

Step 2: Create an HTML Template

Let’s make a simple Knockout-powered app.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Knockout.js Example</title>
</head>
<body>
    <h2>Knockout.js is awesome!</h2>
    <p>Type something: <input data-bind="value: userInput" /></p>
    <p>You typed: <strong data-bind="text: userInput"></strong></p>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-min.js"></script>
    <script>
        function ViewModel() {
            this.userInput = ko.observable('');
        }

        ko.applyBindings(new ViewModel());
    </script>
</body>
</html>

Boom. You now have two-way data binding. Whatever you type in the input field updates the <strong> element automatically. No need to manually listen for events.

Observables: The Secret Sauce

Knockout uses observables to track changes. An observable is just a fancy way of saying, “Hey, JavaScript, keep an eye on this value.”

1
2
3
4
5
let name = ko.observable("John Doe");
console.log(name()); // John Doe

name("Jane Doe");
console.log(name()); // Jane Doe

Observables allow Knockout to magically update the UI whenever your data changes. It’s like having a built-in assistant who updates everything for you.

Computed Observables: Fancy Stuff

What if you want to calculate something dynamically? Use computed observables.

1
2
3
4
5
function ViewModel() {
    this.firstName = ko.observable("John");
    this.lastName = ko.observable("Doe");
    this.fullName = ko.computed(() => this.firstName() + " " + this.lastName());
}

Now, fullName will always update when firstName or lastName changes. Pretty neat, huh?

Knockout.js vs. The Modern World

Look, I get it. Knockout isn’t the newest, shiniest thing on the block. It doesn’t have Virtual DOM. It doesn’t have a React-like component system. But it still has its place.

  • Need a lightweight, dependency-free solution for a simple project? Use Knockout.
  • Working on a legacy project that already uses Knockout? Stick with it.
  • Want to impress your friends with JavaScript trivia? Tell them about Knockout.

Key Ideas

ConceptSummary
Knockout.jsA lightweight JavaScript library for MVVM.
Data BindingAutomatic synchronization between UI and data.
ObservablesJavaScript variables that auto-update the UI.
Computed ValuesDerived values that update dynamically.
SimplicityNo dependencies, easy setup, and still relevant.

References